int vs. long

Started by
28 comments, last by Densun 22 years, 1 month ago
quote:Original post by Anon Mike
You''re not even guaranteed that sizeof(char)==1 (8-bit) *byte*. You''re only guaranteed that it is 1 *something*.


Yup, there is no guarantee that a byte is 8 bits. There were machines with 9-bit or 13-bit bytes (still true in some embedded systems).

"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
The previous AP up there was fairly confused about the C/C++ standards. As I said, int is guaranteed at least 16 bits, long is guaranteed at least 32 and at least the same size as int.

It''s true that ''int'' is typically the most efficient integer representation for the machine (not guaranteed, but true for every machine I''ve ever heard of). Long being 64 bits on X86 would be compiler dependent; MSDev''s is 32, but others may do it differently.

--
Eric
-- Eric
quote:Original post by ekenslow
The previous AP up there was fairly confused about the C/C++ standards. As I said, int is guaranteed at least 16 bits, long is guaranteed at least 32 and at least the same size as int.

Guaranteed by what?


  1. Objects declared as characters (char) shall be large enough to store any member of the implementation’s
    basic character set. [...]

  2. There are four signed integer types: “signed char”, “short int”, “int”, and “long int.” In this
    list, each type provides at least as much storage as those preceding it in the list. Plain ints have the natural
    size suggested by the architecture of the execution environment39) ; the other signed integer types are
    provided to meet special needs.

  3. For each of the signed integer types, there exists a corresponding (but different) unsigned integer type:
    “unsigned char”, “unsigned short int”, “unsigned int”, and “unsigned long
    int,” each of which occupies the same amount of storage and has the same alignment requirements (3.9)
    as the corresponding signed integer type) ; that is, each signed integer type has the same object representation
    as its corresponding unsigned integer type. [...]


39that is, large enough to contain any value in the range of INT_MIN and INT_MAX, as defined in the header <climits>.


long is guaranteed to be at least as large as int, but there is no guarantee that it won't be the same as short, or even char.


Edited by - DrPizza on February 23, 2002 1:30:42 AM
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/
quote:Original post by Anon Mike
You're not even guaranteed that sizeof(char)==1 (8-bit) *byte*. You're only guaranteed that it is 1 *something*.


The Standard actually does guarantee that sizeof(char) is 1 byte, because the sizeof operator "yields the size (in bytes) of its operand" [C89 6.3.3.4, C99 6.5.3.4].

Edited by - spock on February 23, 2002 6:43:02 PM
quote:Original post by ekenslow
The previous AP up there was fairly confused about the C/C++ standards. As I said, int is guaranteed at least 16 bits, long is guaranteed at least 32 and at least the same size as int.


There are no such guarantees. This has been pointed out by various posters to this thread about half a dozen times, with accompanying quotes from the Standard. Why is it so hard for you people to understand?

--

The placement of a donkey''s eyes in its head enables it to see all four feet at all times.
long does have a gaurantee, I read it in Stroustrup''s book. I don''t think short has one. So I think the only numerical gaurantees are that sizeof(char) is one and that sizeof(long) is at least 4. Then there is the chain of greater than or equals to, and a secondary one with bool in it. (I''m a different AP btw)

Anyway to answer the original poster''s question: we use ints because they are natural. They are defined to be the best all purpose integral type. We have longs as an option, but guess what, on most computers (including all windows computers) longs are exactly the same size as ints. There is no difference except that "long" looks funny. I think long long actually is twice as big but if long has worked so far that means int will too, and it will run faster and use less memory than using silly long longs.
Ok, I don''t have a copy of the C++ standard, but according to The C++ Programming Language, Special Edition, page 75:
"In addition, it is guaranteed that a char has at least 8 bits, a short at least 16 bits, and a long at least 32 bits."

Is this in conflict with what the standard actually says?
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
quote:Original post by Martee
Ok, I don''t have a copy of the C++ standard, but according to The C++ Programming Language, Special Edition , page 75:
"In addition, it is guaranteed that a char has at least 8 bits, a short at least 16 bits, and a long at least 32 bits."

Is this in conflict with what the standard actually says?


Evidently, since that is what I was quoting above.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/
quote:Original post by DrPizza
Guaranteed by what?


By C89 §5.2.4.2.1 Sizes of integral types . The standard(s) does define the minimum number of bits for char to 8 and minimum ranges for other integral types. These minimum ranges effectively defines the minimum sizes in bits.

The sizes are implementation-defined, but char must have at least 8 bits, int must have at least 16 bits, and long must have at least 32 bits. (C99 defines the minimum size of long long to 64 bits.)
I think everybody should try using MSVC''s
__int8
__int16
__int32
__int64

Would help, doncha think?

-----------------------------
Damnit Dave, I would have a link to Graphics Wars here, but you haven''t put it up on GDNet yet!

The sad thing about artificial intelligence is that it lacks artifice and therefore intelligence.

Democracy is where you say what you want and do what you''re told.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.

This topic is closed to new replies.

Advertisement