/* * sizeof() cheat sheet. * Compile on a particular architecture/compiler/ABI combination * to learn the sizes of different structures and constants. * * Written by Godmar Back for CS 3204 Fall 2006. */ #include #define PRINT_SIZEOF(p) do { printf("sizeof (%s) -> %d\n", #p, sizeof(p)); } while (0) int main() { PRINT_SIZEOF(char); PRINT_SIZEOF(unsigned char); PRINT_SIZEOF(short); PRINT_SIZEOF(void); // gcc only PRINT_SIZEOF(int); PRINT_SIZEOF(unsigned); PRINT_SIZEOF(long); PRINT_SIZEOF(long long); PRINT_SIZEOF(unsigned long long); PRINT_SIZEOF(float); PRINT_SIZEOF(double); PRINT_SIZEOF(long double); PRINT_SIZEOF(_Bool); // defined by C99 PRINT_SIZEOF(512); PRINT_SIZEOF('\0'); PRINT_SIZEOF((char)'\0'); PRINT_SIZEOF('0'); PRINT_SIZEOF(512L); PRINT_SIZEOF(512LL); PRINT_SIZEOF(512ULL); PRINT_SIZEOF(512UL); PRINT_SIZEOF(512LU); PRINT_SIZEOF(3.0f); PRINT_SIZEOF(3.0); PRINT_SIZEOF(3.0l); PRINT_SIZEOF(void *); PRINT_SIZEOF(void **); PRINT_SIZEOF(char *); PRINT_SIZEOF(int *); PRINT_SIZEOF(unsigned *); PRINT_SIZEOF(long double *); PRINT_SIZEOF(struct { int x; }); PRINT_SIZEOF(struct { char x; int y; }); PRINT_SIZEOF(struct { char x; short y; int z; }); PRINT_SIZEOF(struct { char w; short x; char y; int z; }); PRINT_SIZEOF(struct { char w; short x; char y; int z; }*); PRINT_SIZEOF(struct { char w; short x; char y; int z; }**); PRINT_SIZEOF(struct { char x; double y; }); PRINT_SIZEOF(struct { char x; float y; }); PRINT_SIZEOF(char [22]); char declared_as_char_buf_30[30]; PRINT_SIZEOF(declared_as_char_buf_30); PRINT_SIZEOF("0123456789"); PRINT_SIZEOF("0123456789" + 0); PRINT_SIZEOF(*"0123456789"); PRINT_SIZEOF("0123456789"[5]); char *declared_as_char_pointer; PRINT_SIZEOF(declared_as_char_pointer); PRINT_SIZEOF(*declared_as_char_pointer); PRINT_SIZEOF(int [512]); PRINT_SIZEOF(unsigned short [256]); PRINT_SIZEOF(unsigned short *[256]); PRINT_SIZEOF(char [512]); PRINT_SIZEOF(struct { int x[126]; int y; int z; }); }