I noticed something strange after compiling this code on my machine:
#include <stdio.h>
int main()
{
printf("Hello, World!\n");
int a,b,c,d;
int e,f,g;
long int h;
printf("The addresses are:\n %0x \n %0x \n %0x \n %0x \n %0x \n %0x \n %0x \n %0x",
&a,&b,&c,&d,&e,&f,&g,&h);
return 0;
}
The result is the following. Notice that between every int address there is a 4-byte difference. However between the last int and the long int there is a 12-byte difference:
Hello, World!
The addresses are:
da54dcac
da54dca8
da54dca4
da54dca0
da54dc9c
da54dc98
da54dc94
da54dc88
int
afterh
in the source code. The compiler may put it in the gap, beforeh
.sizeof
function for that.printf("size: %d ", sizeof(long));
%x
. Lucky for you, it happens to work correctly on your platform to pass pointer args with a format string expectingunsigned int
, but pointers and ints are different sizes in many ABIs. Use%p
to print pointers in portable code. (It's easy to imagine a system where your code would print upper/lower halves of the first 4 pointers, instead of lower half of all 8.)%zu
. @yoyo_fun to print addresses use%p
. Using the wrong format specifier invokes undefined behavior