codegurukool.com · Free coding tutorials & online compiler

Home › C Tutorial › C struct padding

C struct padding

Hidden gaps for alignment.

The compiler adds padding so fields sit on aligned addresses. Ordering fields from large to small can shrink the struct.

Example code

#include <stdio.h>
struct A { char c; int i; char d; };
struct B { int i; char c; char d; };
int main() {
  printf("%zu %zu\n", sizeof(struct A), sizeof(struct B));
  return 0;
}

Run this code in the online editor

← C typedefC enums →