I have looked through a post of structure padding in geeksforgeeks, https://www.geeksforgeeks.org/is-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member/ But I don't know why in this case:
int main()
{
struct C {
// sizeof(double) = 8
double z;
// sizeof(short int) = 2
short int y;
// Padding of 2 bytes
// sizeof(int) = 4
int x;
};
printf("Size of struct: %d", sizeof(struct C));
return 0;
}
I know y (short int) is followed by x (int) and hence padding is required after y. But why the padding here is 2?