delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/12/05/17:05:25

From: George Foot <mert0407 AT sable DOT ox DOT ac DOT uk>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Integers (4bytes VS 2bytes)
Date: 5 Dec 1997 18:59:10 GMT
Organization: Oxford University, England
Lines: 57
Message-ID: <669itu$jpg$1@news.ox.ac.uk>
References: <3486F1EC DOT CBE AT ok DOT com>
NNTP-Posting-Host: sable.ox.ac.uk
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

On Thu, 04 Dec 1997 10:09:48 -0800 in comp.os.msdos.djgpp thanks AT ok DOT com wrote:

: I have a quick question.  I just downloaded DJGPP and I see that
: the integers are 4 bytes not 2 bytes, like other compilers.  The
: question is why?

`int' is meant to be the basic integer type for the platform -- the
one it "prefers" using.  DJGPP programs run in a 32-bit environment,
so their `int' type is 32 bits long (4 bytes).  The other compilers
you refer to are presumably compilers for 16-bit DOS, so they have
16-bit integers by default.  Other compilers from different platforms
can have very different sizes for all types (64-bit ints, 9-bit chars,
etc).  I believe the only constraints are that [1] `short' is no
larger than `int' which is no larger than `long', [2] `short' is at
least 16 bits, and [3] `long' is at least 32 bits -- but I could be
wrong here.

You can get a 16-bit int type by using `short' (or `short int')
instead of just `int':

int this_is_32_bits_in_djgpp;
long so_is_this;
short this_is_16_bits;
long long this_is_64_bits;

`long long' is not an ANSI standard type.

Some say that in theory, though, you should use `int' only when you
don't really care how long the number is, using `short' or `long'
where you do care.  I personally don't think this helps a great deal
because the sizes of all these types can change from platform to
platform; another option is to use `int16', `int32', etc, for types of
a specific length, and #define or typedef these as appropriate for the
compiler in use, e.g.:

typedef short int16
typedef long int32
typedef signed char int8

This is useful because in porting your code you only need to change
the type sizes in one place.

: Also, I created a binary file using TC++ and 
: would like to know if there's any problem with reading in the
: integers from it.

You should always be careful when using binary files in this way; the
safest way which is portable across different platforms (I mean the
files can be transfered from one to another more than anything else)
is to read and write them byte by byte.  This also circumvents any
endianness problem.

If you're wanting to read in 16-bit values, though, you should be OK
reading them straight into short ints.

-- 
george DOT foot AT merton DOT oxford DOT ac DOT uk

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019