integer size in c depends on what?
Size of the integer depends on what?
Is the size of an int variable in C dependent on the machine or the compiler?
Solution 1:
It's implementation-dependent. The C standard only requires that:
-
char
has at least 8 bits -
short
has at least 16 bits -
int
has at least 16 bits -
long
has at least 32 bits -
long long
has at least 64 bits (added in 1999) - sizeof(char) ≤ sizeof(short) ≤ sizeof(int) ≤ sizeof(long) ≤ sizeof(long long)
In the 16/32-bit days, the de facto standard was:
-
int
was the "native" integer size - the other types were the minimum size allowed
However, 64-bit systems generally did not make int
64 bits, which would have created the awkward situation of having three 64-bit types and no 32-bit type. Some compilers expanded long
to 64 bits.
Solution 2:
Formally, representations of all fundamental data types (including their sizes) are compiler-dependent and only compiler-dependent. The compiler (or, more properly, the implementation) can serve as an abstraction layer between the program and the machine, completely hiding the machine from the program or distorting it in any way it pleases.
But in practice compilers are designed to generate the most efficient code for given machine and/or OS. In order to achieve that the fundamental data types should have natural representation for the given machine and/or OS. In that sense, these representations are indirectly dependent on the machine and/or OS.
In other words, from the abstract, formal and pedantic point of view the compiler is free to completely ignore the data type representations specific to the machine. But it makes no practical sense. In practice compilers make full use of data type representations provided by the machine.
Still, if some data type is not supported by the machine, the compiler can still provide that data type to the programs by implementing its support at the compiler level ("emulating" it). For example, 64-bit integer types are normally available in 32-bit compilers for 32-bit machines, even though they are not directly supported by the machine. Back in the day the compilers would often provide compiler-level support for floating-point types for machines that were not equipped with floating-point co-processor (and therefore did not support floating-point types directly).