🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to C Notes
Topic #78

C Struct Padding


Struct Padding

When you create a struct in C, the compiler may add some extra bytes of padding between members.

This is done to make the program run faster on your computer, because most CPUs read data more efficiently when it's properly aligned in memory.

You rarely need to worry about this unless you work with low-level memory or file formats.

Note: In short: Padding means the compiler sometimes adds empty spaces inside a struct to keep things fast and properly aligned in memory.

Let's look at a simple struct:

Example

struct Example {
  char a;   // 1 byte
  int b;  // 4 bytes
  char c;   // 1 byte
};

int main() {
  printf("Size of struct: %zu bytes\n", sizeof(struct Example));
  return 0;
}

You might expect the size to be 1 + 4 + 1 = 6 bytes - but it will usually print 12 bytes!

Why?

The compiler adds padding bytes so that the int member (b) starts at a memory address that's a multiple of 4. This helps the CPU read it faster.

Here's how memory is actually arranged:

Member Bytes Notes
a 1 Stored first
padding 3 Added so b starts at a multiple of 4
b 4 Aligned to 4-byte boundary
c 1 Stored next
padding 3 Added to make total size a multiple of 4

Total = 1 + 3 + 4 + 1 + 3 = 12 bytes.


How to Reduce Padding

Padding depends on the order of members in the struct. If you group larger types first, you can make the struct smaller:

Example

struct Example {
  int b;  // 4 bytes
  char a;   // 1 byte
  char c;   // 1 byte
};

int main() {
  printf("Size of struct: %zu bytes\n", sizeof(struct
Example));  // Usually 8 bytes
  return 0;
}

Now the struct is only 8 bytes instead of 12, because there's less padding needed.


Why It Matters

  • Padding helps programs run faster by keeping data aligned in memory.
  • It can make structs bigger than expected.
  • Reordering members can reduce the total size.
  • Note: You rarely need to worry about this unless you work with low-level memory or file formats.

Structs vs Unions

So far, we've talked about padding inside structs. But what about unions?

Unions store all members in the same memory location, so there is no padding between members. However, both structs and unions still follow alignment rules - the data must start at memory addresses that match their type size.

Example

struct S {
  char a;
  int b;
  char c;
};

union U {
  char a;
  int b;
  char c;
};

int main() {
  printf("Struct size: %zu\n", sizeof(struct S));
  printf("Union size: %zu\n", sizeof(union U));
  return 0;
}

Explanation:

  • Struct - members are stored one after another, so padding is added between them.
  • Union - all members share the same memory, so only the largest member decides its total size.
Feature Struct Union
Members stored One after another All share the same memory space
Padding between members Yes No
Aligned to Each member's type Largest member's type
Total size Sum of members + padding Size of largest member

Tip: You usually don't need to worry about this, but it helps to know why structs sometimes take up more memory than expected, while unions do not.


Summary

  • Struct members are aligned based on their data type size.
  • The compiler may add padding bytes to align data properly.
  • The total struct size is often larger than the sum of its members.
  • Reordering members can reduce padding and save memory.

Want to go beyond the notes?

Join CodingNow 2.0's C course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

C Struct Padding – FAQs

Quick answers about learning C Struct Padding in C.

This free note from CodingNow 2.0 explains C Struct Padding in C — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every C topic on CodingNow 2.0, including C Struct Padding, is 100% free with no signup required.
With focused practice, most students grasp C Struct Padding in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now