sizeof()
April 3, 2008 Leave a comment
This function in the stdio library allows you to view the size of different data types in C. This infomration can be important because the size of the various data types in C can vary depending on your hardware, compiler, and os.
For example, take the example program.
#include <stdio.h> int main() { printf( "bool = %u\nshort = %u\nint = %u\nlong = %u\n", sizeof(bool), sizeof(short), sizeof(int), sizeof(long)); return 0; }
When run on my office machine, an amd64 with a 64 bit linux os, it produces the following:
bool = 1
short = 2
int = 4
long = 8
When run on the macbook I’m on now running macosx (note that it has a core 2 duo, which is also 64 bit architecture) it produces the following:
bool = 1
short = 2
int = 4
long = 4
Another common thing – on some windows compilers int will be set to 2 bytes which seems fairly weird to me, but I guess if you lived in that day and age… whatever.