|
-
November 17th, 2009, 11:40 AM
#1
integer type, still machine dependent?
Hello everyone,
I hope my question is not so silly, but when I started to learn C-language 10 years ago, books was mentioning that the data type "int" is machine dependent, and this was when there was a change from 16 to 32 bit machines. Now we are in the move between 32 and 64 bit, is "int" still machine dependent? If yes, why does it exist if we can rely on short and long which have a fixed size?
Many thanks in advance.
-
November 17th, 2009, 11:44 AM
#2
Re: integer type, still machine dependent?
All three of those types have machine-dependent size. The only guarantees I know of in the standard are 1) char can at least handle a numeric range equivalent to 8 bits, 2) long can handle at least a numeric range equivalent to 32 bits, and 3) sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long).
If you need your type to have a specific size, use one of the sized types like int32_t. The sized types are in the C99 standard, and they'll probably make it into the C++ standard eventually too.
-
November 17th, 2009, 11:59 AM
#3
Re: integer type, still machine dependent?
The fact that the basic types (amongst other things) are machine dependent are one of the reasons people use C
-
November 17th, 2009, 12:12 PM
#4
Re: integer type, still machine dependent?
Hi,
Thanks a lot for trying to help.
I'm trying to write clean code, and I'm confused. So when should we use each of them?
monarch_dodra, I don't get it, what's the advantage?
-
November 17th, 2009, 12:17 PM
#5
Re: integer type, still machine dependent?
This discussion came up a few days ago. http://www.codeguru.com/forum/showthread.php?t=488068
The general upshot is that all types are machine dependent in size. The order of sizes is strictly defined. If you have requirements for the size of your variables you're safest bet is to use sizeof() to test the sizes of each type (remember there is a "long long" type supported by many compilers, not all). Alternatively most compilers support __int8 etc to make it unambiguous.
Signature
Please use: [ code ][/ code ] tags and reasonably correct tabbing. They really help us read your code
End Signature
-
November 17th, 2009, 12:24 PM
#6
Re: integer type, still machine dependent?
 Originally Posted by Ahmedg
Hello everyone,
I hope my question is not so silly, but when I started to learn C-language 10 years ago, books was mentioning that the data type "int" is machine dependent,
From what I remember, nothing stops a compiler from "faking out" any number of bits it wants to for an int. For example, I was using C and C++ compilers that had 32-bit ints when programming for 16-bit MSDOS. Special run-time library routines were responsible in getting the 32-bit ints to work in this mode.
Of course, it is more natural for a compiler to support the actual bitness of the machine, but nothing really stops a compiler from implementing 64 bit ints, 128 bit ints, etc. regardless of the machine.
Regards,
Paul McKenzie
-
November 17th, 2009, 12:57 PM
#7
Re: integer type, still machine dependent?
I don't think anything is machine dependent, I think you mean compiler dependent.
int in MinGW is always 32-bit, regardless of machine. Most C++ compilers have their ints 32-bit, it's just not assured. The best thing to do it do define your own int type in preprocessor
Code:
#define int_32 int
ASSERT(sizeof(int_32) == 4, "Oops, add a #if for this platform")
-
November 17th, 2009, 01:00 PM
#8
Re: integer type, still machine dependent?
Good use for a boost::static_assert there.
-
November 17th, 2009, 01:04 PM
#9
Re: integer type, still machine dependent?
Not sure how many compilers support it, but GNU does:
int32_t
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|