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.
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).
Videos
What differentiates the range for unsigned and signed types?
Why do we use the floating data types?
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 :)
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?
On further reading I see you wanted bit sizes that are not normal ones, such as 7 bit and 9 bit etc. You can achieve this using bitfields
struct bits9
{
int x : 9;
};
Now you can use this type bits9 which has one field in it x that is only 9 bits in size.
struct bits9 myValue;
myValue.x = 123;
Using built in types you have things like:
char value1; // 8 bits
short value2; // 16 bits
long value3; // 32 bits
long long value4; // 64 bits
Note this is the case with Microsoft's compiler on Windows. The C standard does not specify exact widths other than "this one must be at least as big as this other one" etc. If you only care about a specific platform you can print out the sizes of your types and use those once you have figured them out.
Alternatively you can use stdint.h which is in the C99 standard. It has types with the width in the name to make it clear
int8_t value1; // 8 bits
int16_t value2; // 16 bits
int32_t value3; // 32 bits
int64_t value4; // 64 bits
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!
So, I ask this question because in my C class at college, my professor said that in the C standard, only relative size of char short and int are specified to keep the language flexible to any hardware architecture setup. sizeof char <= short <= int. ChatGPT seems to have confirmed this saying:
Relative Size: In C, data types such as
char,short,int, andlongare defined in terms of their relative sizes. The C standard specifies that:However, the absolute size (in terms of number of bits or bytes) of these types is not fixed by the C standard. For example, the size of anintorshortcan vary depending on the architecture or the compiler. This flexibility allows C to work across different systems.
sizeof(char)must always be 1 byte (this is guaranteed),
sizeof(short)must be at least as large assizeof(char),
sizeof(int)must be at least as large assizeof(short),
sizeof(long)must be at least as large assizeof(int).
Absolute Size: The actual size in terms of bits can vary by platform. For example, on many modern systems:
charis usually 8 bits,
shortis often 16 bits,
intcan be 16 or 32 bits (commonly 32 bits nowadays),
longcan be 32 or 64 bits (often 64 bits on modern systems).
Technical Point from Your Professor: Your professor is correct in saying that, technically, all of these types could be 9 bits, as long as the relative size constraints are followed. This would be unusual, but C is designed to be flexible and portable across different architectures. The key is that the relative sizes of types must adhere to the standard's requirements, but the absolute size of the types may vary.
However, on the Wikipedia page for C data types:
The actual size of the integer types varies by implementation. The standard requires only size relations between the data types and minimum sizes for each data type:
The relation requirements are that the
long longis not smaller than long, which is not smaller than int, which is not smaller than short. As char's size is always the minimum supported data type, no other data types (except bit-fields) can be smaller.
The minimum size for char is 8 bits, the minimum size for short and int is 16 bits, for long it is 32 bits and
long longmust contain at least 64 bits.
Can someone tell what is correct and perhaps provide a definitive source? Maybe I am misunderstanding something or my professor is working with outdated information.