Basically, to illustrate how this add-on works, I slightly modified your program.
#include<stdio.h> struct a { int x; char y; int z; }; int main() { struct a str; str.x=2; str.y='s'; str.z = 13; printf ( "sizeof(int) = %lu\n", sizeof(int)); printf ( "sizeof(char) = %lu\n", sizeof(char)); printf ( "sizeof(str) = %lu\n", sizeof(str)); printf ( "address of str.x = %p\n", &str.x ); printf ( "address of str.y = %p\n", &str.y ); printf ( "address of str.z = %p\n", &str.z ); return 0; }
Notice that I added the third element to the structure. When I run this program, I get:
amrith@amrith-vbox :~/so$ ./padding sizeof(int) = 4 sizeof(char) = 1 sizeof(str) = 12 address of str.x = 0x7fffc962e070 address of str.y = 0x7fffc962e074 address of str.z = 0x7fffc962e078 amrith@amrith-vbox :~/so$
Part of this, which illustrates the filling, is highlighted below.
address of str.y = 0x7fffc962e074 address of str.z = 0x7fffc962e078
As long as y is just one character, note that z is the full 4 bytes.
source share