What is structure padding in C?

The compiler adds empty bytes (padding) between structure members so that each member starts at an address that suits its type. So the size of a structure can be more than the sum of its members.

#include <stdio.h>

struct A { char a; int b; char c; };
struct B { int b; char a; char c; };

int main(void) {
    printf("%zu\n", sizeof(struct A));
    printf("%zu\n", sizeof(struct B));
    return 0;
}

Output:

Output of the above program is

12
8

Both have the same members, but the order in struct B wastes less space. A good habit is to put the bigger members first.


Learn more about the similar topics:
Tutorials
C Structures
C Nested Structure
C Array of Structures
C Pointer to Structure
C Union
C typedef
C enum
Exercises & Assignments
Write a C program to store and print student details using a structure
Interview Questions & Answers
What is the difference between structure and union in C?
What is typedef in C?
What is an enum in C?
What is structure padding in C?