I know it's equal to sizeof(int). The size of an int is really compiler dependent. Back in the day, when processors were 16 bit, an int was 2 bytes. Nowadays, it's most often 4 bytes on a 32-bit as well as 64-bit systems.
Still, using sizeof(int) is the best way to get the size of an integer for the specific system the program is executed on.
EDIT: Fixed wrong statement that int is 8 bytes on most 64-bit systems. For example, it is 4 bytes on 64-bit GCC.
I know it's equal to sizeof(int). The size of an int is really compiler dependent. Back in the day, when processors were 16 bit, an int was 2 bytes. Nowadays, it's most often 4 bytes on a 32-bit as well as 64-bit systems.
Still, using sizeof(int) is the best way to get the size of an integer for the specific system the program is executed on.
EDIT: Fixed wrong statement that int is 8 bytes on most 64-bit systems. For example, it is 4 bytes on 64-bit GCC.
This is one of the points in C that can be confusing at first, but the C standard only specifies a minimum range for integer types that is guaranteed to be supported. int is guaranteed to be able to hold -32767 to 32767, which requires 16 bits. In that case, int, is 2 bytes. However, implementations are free to go beyond that minimum, as you will see that many modern compilers make int 32-bit (which also means 4 bytes pretty ubiquitously).
The reason your book says 2 bytes is most probably because it's old. At one time, this was the norm. In general, you should always use the sizeof operator if you need to find out how many bytes it is on the platform you're using.
To address this, C99 added new types where you can explicitly ask for a certain sized integer, for example int16_t or int32_t. Prior to that, there was no universal way to get an integer of a specific width (although most platforms provided similar types on a per-platform basis).
Hai there, I had an embedded software exam today where one of the questions stated:
The C language is centered around the int data type that represents the canonical machine word.
- As such the size of an int is architecture dependent.
And the answer to this true/ false question was true. Now I understand that's the answer they were fishing for, but I made the frankly stupid decision to be pedantic so now I need to down the rabbit hole to see if I'm right.
In my understanding, while the int type is architecture dependent (although I'm not 100% certain that's specified), it does not represent the canonical machine word. On my x86_64 machine, int is 32 bits, not 64, and I know that int cannot be less than 16 bits, so on 8 bit processors cannot have int be their word size.
Looking around online, I've found a stack overflow answer that the relation to machine words are more a suggestion rather than a rule. However that did not link to a part of the C spec.
I made an attempt looking in the C24 draft spec (that one was free) but wasn't able to find any useful information quickly in ~700 pages, outside the fact that the minimum size is indeed 16 bits.
So my concrete question: where, if anywhere, in the C spec can I find what the C programming language defines as the size of the int type and if it's at all in relation to word size of a particular architecture, so I can disprove either my professor or myself.
Thank you in advance :)
Videos
What differentiates the range for unsigned and signed types?
Why do we use the floating data types?
I know it's equal to sizeof(int). The size of an int is really compiler dependent. Back in the day, when processors were 16 bit, an int was 2 bytes. Nowadays, it's most often 4 bytes on a 32-bit as well as 64-bit systems.
Still, using sizeof(int) is the best way to get the size of an integer for the specific system the program is executed on.
EDIT: Fixed wrong statement that int is 8 bytes on most 64-bit systems. For example, it is 4 bytes on 64-bit GCC.
EDIT: Thank you all, I understand now!
I'm looking at Wikipedia for C data types: https://en.wikipedia.org/wiki/C_data_types
and it says that for an integer, the minimum size in bits is 16.
So I use sizeof on an int variable as such:
int main(){
int c;
int c_size = sizeof(c);
printf("Size of int a is %d", c_size);
scanf(">");
}And when I compile it, it returns 4. What is 4? If it's bytes, then that would be 32 bits, not 16.
Would someone be able to tell me what my misunderstanding is?
The C standard doesn't specify 32 bits for int, it specifies "at least" 16. https://stackoverflow.com/questions/589575/what-does-the-c-standard-state-the-size-of-int-long-type-to-be
In practice, most implementations since the 32-bit machine era picked "int" to be 32 bits, but C implementations for 16 bit machines used 16 bit ints.
"float" appears to also have been in present in K&R C, before the modern standardised ANSI C, so it's been around since the 80s. I can't confirm that the first compiler did not have float.
(An even worse situation is the size of pointers; this has always been implementation-defined, but in the MS-DOS era you could have "near" (16 bit) and "far" (32 bit) pointers and get different answers depending on compiler options)
My first edition of K&R C defines an 'int' as the natural word length of the machine. Which made sense back then because there were a lot of different word lengths on the machines of the time. Everything from 12 to 36 bits were available.
My question is what, if anything, does the C standard have to say about the size of an int?
A ββplainββ int object has the natural size suggested by the architecture of the execution environment (large enough to contain any value in the range INT_MIN to INT_MAX as defined in the header <limits.h>).
and
Their implementation-defined values shall be equal or greater in magnitude (absolute value) to those shown, with the same sign.
β minimum value for an object of type int INT_MIN -32767 // β(2^15 β 1)
β maximum value for an object of type int INT_MAX +32767 // 2^15 β 1
so you can't have an 8-bit int on a compliant implementation.
According to the C standard ISO/IEC 9899 (latest is 2018) the size of an integer is implementation defined.
Likewise, the size of char is likewise implementation define - not necessarily 8 bits!