Results 1 to 8 of 8
Thread: sizeof(int)
|
Enjoy an ad free experience by logging in. Not a member yet? Register.
|
|
-
09-16-2005 #1
- Join Date
- Feb 2005
- Location
- India
- Posts
- 24
sizeof(int)
When I started learning C from a book, it was mentioned that sizeof(int) is 2 bytes. I believed it blindly and never bothered to try it!!
But when I did, I was surprised to see that the sizeof(int) is actually 4 bytes (the same as that of long int). I was told that it was because I tried on a 32-bit machine! I know that on a 16-bit machine it is 2 bytes, as it should be!!
Can neone explain how the sizeof(int) depends on the how_many-bit machine I'm using, while the sizeof(other_datatypes) don't change??
Also how much will the sizeof(int) be on a 64-bit machine??
-
09-16-2005 #2
- Join Date
- Aug 2005
- Location
- Hell
- Posts
- 514
The C standard doesn't specify the sizes of the integer types, only some vague relations between them; and the actual sizes differ between implementation choices; see http://en.wikipedia.org/wiki/C_varia...larations#Size
-
09-16-2005 #3
An int is usually the size of a single register on the processor on which your software is built to run. So for 16 bit operating systems (e.g. Windows 3.1, MSDOS), that'll be 16 bits, for 64 bit operating systems, its 64 bits.
You really shouldn't be relying on integers being a specific size, if you want to use fixed-size integers, then you'll find there are other declarations that you can use, but take care here, some of these can be compiler-based.Linux user #126863 - see http://linuxcounter.net/
-
09-16-2005 #4
- Join Date
- Aug 2005
- Location
- Italy
- Posts
- 401
inttypes.h
Uses declarations from inttypes.h
This header defines symbols wich are architecture independent...
uint32_t -> unsigned 32 bit integer
int8_t -> signed 8 bit integer
...When using Windows, have you ever told "Ehi... do your business?"
Linux user #396597 (http://counter.li.org)
-
09-16-2005 #5
- Join Date
- Oct 2004
- Location
- /dev/random
- Posts
- 404
Originally Posted by Roxoff
sparc64 is a 64-bit CPU - the linux kernel needs to be compiled explicitly for 64-bit on it whereas all user-space apps are 32-bit natively (they use the sparc32 ABI). Check this link for details:
http://www.ultralinux.org/faq.html#q_1_5The Unforgiven
Registered Linux User #358564
-
09-17-2005 #6
- Join Date
- Feb 2005
- Posts
- 1,044
I was always led to believe that a C int was the "natural" word length of the machine, but all the 64-bit implementations I've seen have 32 bit ints. As noted elsewhere, all that the C spec guarantees is that a short is no longer than a long.
-
09-19-2005 #7
- Join Date
- Jun 2005
- Location
- Canada, Halifax
- Posts
- 86
sizeof(int)
In C, by definition in an integer must at a minimum be able to represent -32768 to 32767, and hence unsigned 0-65535, larger than this is allowed but not required. For example an eight bit microcontroller must deal with the minimum representation limitations (char would be its "natural" type) and 64-bit compiler support is still in its infancy so a 32-bit int is legal.
-
09-19-2005 #8
- Join Date
- Jun 2005
- Location
- Canada, Halifax
- Posts
- 86
Side note
BTW: Most 32-bit processors support intermediate 64-bit multiplication results. Some chip manufacturers incorrectly use this fact to advertise 32-bit procs as 64-bit when infact they are not. So long as the internal data bus is 32-bit, 64-bit register-to-register transfers and mathematical operations require multiple move instructions and carry bit checking, thus are multi-precision operations and are not the natural processor's 'size.' This isn't so much a problem today as true 64-bit procs with 128-bit product registers exist, however compilier support and testing is still growing, so it is quite likely that your 64-bit proc is running a lot of 32-bit code.