Memory allocation in C

Hi Experts
I need some help in static memory allocation in C. I have a program in which I declared 2 variables, one char array and one integer. I was little surprised to see the addresses of the variables.

First:

int x;
char a[2];
printf("%u %u\n', &x, a);

I got the addresses displayed as : 3219437172 3219437170
The above result is straight forward wherein the array a got 2 bytes allocated.

Second:

When I changed the program as below:

int x;
char a[3];
printf("%u %u\n', &x, a);

I got the addresses displayed as : 3219437172 3219437144
The array a got allocated 28 bytes when the declaration is for 3 bytes.

Why so?

You are making an assumption that because you define the two variables one after another in your source code, the allocated storage for the variables should be adjacent and in the same order without any holes. This is an incorrect assumption.

The fact that the compiler is able to compile this buggy code seems kinda strange as the printf quoting is incorrect...

printf("%u %u\n', &x, a);  // printf format specifier needs double quotes

Fix it and try again and you should get the results expected.