If you are confused about the size of “int” whether it’s 2 bytes or 4 bytes, follow this article’s guidelines.
Is the Size of C “int” 2 Bytes or 4 Bytes
In the early days, when C language was introduced, the one thing that came in developer minds was to make this language as simple as possible. At that time, the computer used a 16-bit processor, thus at that time, the size of “int” was fixed to 2 bytes. As the time passes, the system computing power increases and the developer then changes the size of “int” in C to 4 bytes. The reason for this is to allow C programmers to use large values and develop more power programs on the system.
What Factors Impacts the “int” size in C
There are three factors that impacts the “int” size in C, which are as follows:
1: The processor of a system highly impacts the “int” size. For a 32-bit architecture, the value of “int” size is 4 bytes. The reason is a byte is composed of 8 bits so if you multiply 8 bits/byte with 4 byte, the result will be 32-bit.
2: The operating system version also has a huge impact on “int” size and a 64-Bit operating system will use 4 bytes “int” types. However, this would still depend on other factors like compiler or platform being used.
3: The compiler also affects the “int” size in C and there are compilers that allow C programmers to configure the size of “int” according to their choice.
Different compilers could have different implementations of the C language, meaning they may view the C “int” data type in different ways. Most commonly, a compiler could consider an “int” in the C language to be 2 bytes or 4 bytes in size. A 2 bytes “int” would occupy two bytes of memory, with the values it could store being shown as -32768 to 32767. In contrast, a 4 byte “int” would occupy four bytes of memory, with values displayed as -2147483648 to 2147483647.
However, if we check the size of an “int” in a modern C++ Compiler, it gives us the answer:
#include <stdlib.h>
#include <limits.h>
int main(int argc, char** argv) {
printf("Int_MAX : %d\n", INT_MAX);
printf("int_MIN : %d\n", INT_MIN);
return 0;
}
In the above code, we are determining the size of an “int” using the functions int_MAX() and int_MIN() which shows us that the size of an “int” is 4 bytes.
Output
Most modern compilers implement the C language in such a way that the size of a C “int” is 4 bytes. That said, some compilers do still utilize the 2 bytes “int” size, meaning developers must be sure to check which compiler they are using in order to make sure their program is working correctly.
Conclusion
The exact size of a C “int” data type can vary in accordance with the compiler being used. Most commonly, it is 4 bytes, allowing for a larger range of values to be stored. That said, some compilers can still implement the C language in such a way that the size of a C “int” is 2 bytes. Ultimately, the programmer must be aware of the compiler being used to determine the exact size of a C “int”.