Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT SWIFT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

C Tutorial

C HOME C Intro C Get Started C Syntax C Output C Comments C Variables C Data Types C Constants C Operators C Booleans C If...Else C Switch C While Loop C For Loop C Break/Continue C Arrays C Strings C User Input C Memory Address C Pointers

C Functions

C Functions C Function Parameters C Scope C Function Declaration C Math Functions C Inline Functions C Recursion C Function Pointers

C Files

C Create Files C Write To Files C Read Files

C Structures

C Structures C Nested Structures C Structs & Pointers C Unions C typedef C Struct Padding

C Enums

C Enums

C Memory

C Memory Management

C Errors

C Errors C Debugging C NULL C Error Handling C Input Validation

C More

C Date C Random Numbers C Macros C Organize Code C Storage Classes C Bitwise Operators C Fixed-width Integers

C Projects

C Projects

C Reference

C Reference C Keywords C <stdio.h> C <stdlib.h> C <string.h> C <math.h> C <ctype.h> C <time.h>

C Examples

C Examples C Real-Life Examples C Exercises C Quiz C Compiler C Syllabus C Study Plan C Certificate

C Struct Alignment and 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.

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;
}

Try it Yourself »

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;
}

Try it Yourself »

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;
}

Typical result:

Struct size: 12
Union size: 4

Try it Yourself »

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.


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.