www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/12/09/10:21:31

From: Charles Krug <charles AT mail DOT pentek DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: sizes of ints, and longs.
Date: Wed, 09 Dec 1998 10:01:13 -0500
Organization: Pentek Corporation
Lines: 75
Message-ID: <366E90B9.19F3E154@mail.pentek.com>
References: <366e5924 DOT 0 AT 139 DOT 134 DOT 5 DOT 33>
NNTP-Posting-Host: mail.pentek.com
Mime-Version: 1.0
X-Mailer: Mozilla 4.5 [en] (WinNT; U)
X-Accept-Language: en
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

"Johan Venter (aka sphinX)" wrote:

> Well, in real mode compilers, the data types are normally as follows:
>
>    int : 2 bytes
>    long: 4 bytes

Not necessarily.  ANSI says:

sizeof(short) <= sizeof(int) <= sizeof(long).

nb--all of these CAN be the same size.  I can name several processors where this is the
case.

The other promise of ANSI is that ints are at LEAST 16-bits long.  I'm not aware of any
platform where they are shorter, but I do know of some where they are longer, like djgpp.

An int is NORMALLY the size data that your target platform can handle most efficiently.
Generally, you use int's unless there is some compelling reason to use something else.

In a situation where you MUST know the size of an integer, writing to a memory mapped
register is something I do every day, then you use a matching size.  But that is a platform
specific question.  If you're doing that type of thing you should be REAL familiar with your
target.

> But, I read somewhere that in DJGPP, the types are as follows:
>
>       int: 4 bytes
>    long: 4 bytes

That is correct.  Remember, this IS compliant.

> If the above is correct, then I would be correct in saying that an
> unsigned short is equal to a real mode WORD, and an unsigned int
> (or an unsigned long) would be equal to a real mode DWORD.

In the most general sense, a "word" is a group of one or more bits.  A four-bit word is a
nibble.  An eight-bit word is a byte.  16-bit words are often called words, and 32-bit words
are often called dwords, and so on, but they are all "words" in the broadest sense.

You can have a real mode compiler with 32-bit ints.  In fact, I use a real mode compiler
with 32-bit characters.  This adds an element of confusion, since ANSI calls sizeof(char) a
"byte."  So on this platform, TI's tms320c40 DSP, a "byte" is 32 bits, as counterintuitive
as that may seem.

> Are these assumptions correct or am I missing something?

In general, it's a good idea to not assume anything about your target platform.  There is an
ANSI required header file called limits.c, which contains the allowed ranges for all data
types on the platform.  If you're doing anything where this is critical, you should look in
this file first.

> The reason I asked this question was I was all confused with DJGPP when I first started
> using it because of the size of the types and some code I had studied, but I just wanted
> to confirm these facts.

Alot of introductory programming books assume that you're using C on a particular platform
using a particular compiler.  This assumption is incorrect in your case.  I recommend
"Practical C Programming," part of O'Reilly's "Nutshell" series.  Of all the introductory C
books I've seen, this makes the fewest assumptions about your platform.  Other good choices
are anything that targets an academic audience.  Books used in colleges generally assume a
Unix platform running cc or gcc, which, until you start using Allegro, is alot closer to
what we do with  djgpp.


charles

--
Charles Krug, Jr.
Application Engineer
Pentek Corp
1 Park Way
Upper Saddle River, NJ 07458


- Raw text -


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