From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.2 required=5.0 tests=BAYES_00,INVALID_MSGID, PLING_QUERY autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fdb77,4873305131bf4d94 X-Google-Attributes: gidfdb77,public X-Google-Thread: 109fba,4873305131bf4d94 X-Google-Attributes: gid109fba,public X-Google-Thread: 103376,4873305131bf4d94 X-Google-Attributes: gid103376,public X-Google-Thread: 1014db,4873305131bf4d94 X-Google-Attributes: gid1014db,public From: kaz@helios.crest.nt.com (Kaz Kylheku) Subject: Re: How big is an int? (was: Yet another stupid language war (was: ... the only languages you need!!)) Date: 1997/11/04 Message-ID: <63oadj$ljc$1@helios.crest.nt.com>#1/1 X-Deja-AN: 287060930 References: <34557f2b.1934172@news.mindspring.com> <63anc7$75p$1@darla.visi.com> <345947D2.D20EF8E9@horvath.com> <345F49A2.5F5DC5A0@aom.ericsson.se> Organization: A poorly-installed InterNetNews site Newsgroups: comp.lang.ada,comp.lang.c,comp.lang.c++,comp.lang.java.advocacy,comp.lang.c,comp.lang.c++ Date: 1997-11-04T00:00:00+00:00 List-Id: In article <345F49A2.5F5DC5A0@aom.ericsson.se>, Ted Lyngmo wrote: >Bob Horvath wrote: >> >> Hmmmm. How big is an int? > >As I've understood it, an int has two legal sizes. A short has only one, You haven't understood it. A short is large enough to represent numbers in the range -32767 to 32767. An int is also large enough to represent numbers in the same range. A long can represent numbers in the range -2147483647 to +2147483647. A C implementation can supply larger actual ranges. Each conforming implementation supplies a header that provides constants that tell you what the actual ranges are. For example, some implementations of C for IBM mainframes have 36 bit longs and 9 bit chars. Thus the ranges are actually greater than the minimum requirements. >and the same goes for a long. I believe that there were a lot of >different compilers on the market before C was standardised. Some used 2 >byte int and some used 4 byte ints. > >short = 2 bytes >int = 2 or 4 bytes >long = 4 bytes This table is a familiar part of popular C mythology, and is not grounded in the language definition. For one thing, it is not true that an int has the same representation as either short or long. It's possible for an implementation to have three distinct representations for the three types. For example: 16 bit two's complement short, 32 bit int, and 64 bit long. Knowing the number of bytes required to store a given integral type is not useful to the vast majority of C programming tasks. Any program which depends on a particular size or storage layout is non-portable. It isn't the least bit interesting that an int is 4 bytes wide. On a system with 16 bit bytes, the same 32 bit integer would be only 2 bytes wide. There may be unused bits in an integral type's storage layout---bits which do not contribute to its value. Thus the number of bytes is not directly related to the range of the type. And of course, bytes are not necessarily eight bits wide. A character type (char, signed char, unsigned char) is always one byte wide, however, and all of its bits are value-contributing. The number of bits in a byte is defined by the macro CHAR_BITS in . >If this is true, I see no reason for ever using ints in new C programs. There are lots of good reasons for using int in new C programs. If you don't require the range to be greater than -32767 to 32767, and you are concerned about performance, then int is a great choice. If you are concerned about saving storage in aggregates, then short is your choice. If you require a larger range, but wish to remain portable, you have to switch to the type long. If you don't need to represent quantities beyond -127 to 127, and want to save storage, then you can use char. But doing so may cost you some performance. To save even more storage, you can declare small bit-field structure members. On some modern compilers, the type long is actually a 64 bit type, so you might waste space and memory bandwidth needlessly if you use it without a good reason to do so. -- "In My Egotistical Opinion, most people's C programs should be indented six feet downward and covered with dirt." -- Blair P. Houghton