Reputation: 4363
For example, int
takes 4 bytes on a 32-bit OS, but 8 bytes on a 64-bit OS, does this kind of thing exist in C?
Upvotes: 4
Views: 3986
Reputation: 26094
It can depend on the OS, but more typically it depends on the hardware. Many micro controllers still use 16-bit int:s, since this is what the device handles naturally.
While we're in the subject. This does not only affect the int
type, it also affects how expressions are evaluated. The principle is that every expression is evaluated as though it would have been evaluated in int
precision, or larger. For example, given two 16-bit short
variables a
and b
and a long
variable x
, the expression x = a*b
will yield different result depending on the size of int
, even though none of the involved variables have the type int
.
Upvotes: 3
Reputation: 1444
C datatype sizes depend on the memory model implemented by the compiler. Think in terms of ILP (Int, Long, Pointer), and the number of bits used for those specified.
So compilers using an ILP64 data model will have the Int, Long and Pointer all set to 64 bits. But an LP64 data model will have the Int set to 32bits and the Long and Pointer set to 64 bits.
Compiler writers will try to use a data model that best fits the target platform. But also have to take into consideration the difficulty of porting libraries, and compiling source code intended for a different data model.
You can easily see what data model you are using with the following:
#include <stdio.h>
#define BYTE_SIZE 8
void main(void)
{
int sizeof_int = sizeof(int) * BYTE_SIZE;
int sizeof_long = sizeof(long) * BYTE_SIZE;
int sizeof_ptr = sizeof(&sizeof_int) * BYTE_SIZE;
printf("Size of int: %d, long: %d, pointer: %d\n",
sizeof_int, sizeof_long, sizeof_ptr );
}
On my 64 bit AMD Pc running 64 bit Debian Squeeze, it shows that GCC is using and LP64 data model. This data model is the most common 64 bit data model, as it was agreed to standardize on this by a number of major UNIX vendors in 1995. They did this to aid the transition, ensure portability and interoperability and to maximize performance.
Upvotes: 2
Reputation: 41394
Yes. This is precisely what is meant by "platform-specific definitions" for things like the size of int
and the meaning of system calls.
They depend not just on the OS, but also on the target hardware and compiler configuration.
Upvotes: 6