The ISO C standard says that:
sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)
I am using GCC-8 on BIT Linux mint (19.1) and the size of long int
is 8
.
I am using an app which uses GCC 7 and the compiler is 64-bit.
The size of long int
is 4
.
Does the compiler or the operating system define the size of a long int
?
The compiler calls all the shots. The operating system just runs the resulting binary.
That being said, the compiler will normally make an executable the operating system can use, so there's some interplay here. Since things like the size of int
don't really matter so long as they're consistent, you will see variation.
In other words, if the kernel expects long int
to be 8 bytes because of how it was compiled, then you'll want to compile that way to match or your compiled code won't match and none of the shared libraries will work.
The Application Binary Interface for an operating system/architecture specifies the sizes of basic types:
ABIs cover details such as (bolding mine):
- a processor instruction set (with details like register file structure, stack organization, memory access types, ...)
- the sizes, layouts, and alignments of basic data types that the processor can directly access
- the calling convention, which controls how functions' arguments are passed and return values are retrieved; for example, whether all parameters are passed on the stack or some are passed in registers, which registers are used for which function parameters, and whether the first function parameter passed on the stack is pushed first or last onto the stack
- how an application should make system calls to the operating system and, if the ABI specifies direct system calls rather than procedure calls to system call stubs, the system call numbers
- and in the case of a complete operating system ABI, the binary format of object files, program libraries and so on.
This is left to the discretion of the implementation.
It's the implementation (compiler and standard library) that defines the size of long
, int
, and all other types.
As long as they fit the constraints given by the standard, the implementation can make all the decisions as to what sizes the types are (possibly with the exception of pointers).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With