Padding aligns structure members to "natural" address boundaries - say, int members would have offsets, which are mod(4) == 0 on 32-bit platform. Padding is on by default. It inserts the following "gaps" into your first structure:

struct mystruct_A {
    char a;
    char gap_0[3]; /* inserted by compiler: for alignment of b */
    int b;
    char c;
    char gap_1[3]; /* -"-: for alignment of the whole struct in an array */
} x;

Packing, on the other hand prevents compiler from doing padding - this has to be explicitly requested - under GCC it's __attribute__((__packed__)), so the following:

struct __attribute__((__packed__)) mystruct_A {
    char a;
    int b;
    char c;
};

would produce structure of size 6 on a 32-bit architecture.

A note though - unaligned memory access is slower on architectures that allow it (like x86 and amd64), and is explicitly prohibited on strict alignment architectures like SPARC.

Answer from Nikolai Fetissov on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ structure-member-alignment-padding-and-data-packing
Structure Member Alignment, Padding and Data Packing - GeeksforGeeks
If the short int element is immediately allocated after the char element, it will start at an odd address boundary. The compiler will insert a padding byte after the char to ensure short int will have an address multiple of 2 (i.e.
Published ย  July 29, 2025
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ what is structure padding in c?
What is Structure Padding in C? - Scaler Topics
May 30, 2024 - Structure padding mainly tells us about memory allocation for variables that are aligned in sequence based on the size of the variable. For example, let us assume a โ€œcharโ€ of 1-byte memory can be assigned anywhere between 0x5000 to 0x5001.
Discussions

c - Structure padding and packing - Stack Overflow
Learn more about Collectives ... Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... The sizes of the structures are 12 and 8 respectively. ... @Paolo, that Lost Art link does not show what happens when there is pointer-alignment and the above where two ints might be one after another. ... Padding ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How structure-padding in C/C++ actually works
Aren't pragmas specific to the compiler? What might be pragma pack in one compiler might be pragma unpadded in another. You should also perhaps mention that placing the largest elements first in the struct, then the next largest, and so on ... wastes the least space, because each field is aligned. So interspersing 1-byte fields between 8-byte fields leaves tons of padding after the 1-byte field and before the next 8-byte field. Putting all the 8-byte fields first leaves no padding in between them, and no padding between the last 8-byte field and the first 4-byte field, and all the 4-byte fields will have no padding between them. Same thing happens as you go to the 2-byte fields, finally ending up with all the 1-byte fields, and padding only after the last 1-byte field. IOW, look at the output from the following program: #include #include int main (void) { struct bad_t { uint8_t f1_1; uint32_t f4_1; uint16_t f2_1; uint64_t f64_1; uint8_t f1_2; uint16_t f2_2; uint32_t f4_2; uint64_t f64_2; }; struct good_t { uint64_t f64_1; uint64_t f64_2; uint32_t f4_1; uint32_t f4_2; uint16_t f2_1; uint16_t f2_2; uint8_t f1_1; uint8_t f1_2; }; printf ("bad: %zu bytes\ngood: %zu bytes\n", sizeof (struct bad_t), sizeof (struct good_t)); } // $ gcc -W -Wall tmp.c -o tmp.exe && ./tmp.exe // bad: 40 bytes // good: 32 bytes More on reddit.com
๐ŸŒ r/programming
9
0
May 13, 2024
Is struct padding defined in any C standard?
Here is a draft for C99. Chapter J.3 describes implementation defined behavior. J3.9 says: The alignment of non-bit-field members of structures (6.7.2.1).This should present no problem unless binary data written by one implementation is read by another. So struct member alignment is completely implementation dependent. More on reddit.com
๐ŸŒ r/C_Programming
23
28
August 6, 2019
A question on structure padding
MyStruct is a type that has its own alignment requirements. I am lazy to look it up in the C standard but I think it equals the alignment of the largest member. So, a MyStruct structure would always be stored at a word-aligned address. More on reddit.com
๐ŸŒ r/embedded
22
7
December 20, 2021
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ cprogramming โ€บ c_structure_padding_and_packing.htm
Structure Padding and Packing in C
Structure padding in C is the process that is handled by the CPU architecture. Structure Padding adds a certain number of empty bytes within a structure so that the data members are naturally aligned in memory.
Top answer
1 of 11
404

Padding aligns structure members to "natural" address boundaries - say, int members would have offsets, which are mod(4) == 0 on 32-bit platform. Padding is on by default. It inserts the following "gaps" into your first structure:

struct mystruct_A {
    char a;
    char gap_0[3]; /* inserted by compiler: for alignment of b */
    int b;
    char c;
    char gap_1[3]; /* -"-: for alignment of the whole struct in an array */
} x;

Packing, on the other hand prevents compiler from doing padding - this has to be explicitly requested - under GCC it's __attribute__((__packed__)), so the following:

struct __attribute__((__packed__)) mystruct_A {
    char a;
    int b;
    char c;
};

would produce structure of size 6 on a 32-bit architecture.

A note though - unaligned memory access is slower on architectures that allow it (like x86 and amd64), and is explicitly prohibited on strict alignment architectures like SPARC.

2 of 11
165

(The above answers explained the reason quite clearly, but seems not totally clear about the size of padding, so, I will add an answer according to what I learned from The Lost Art of Structure Packing, it has evolved to not limit to C, but also applicable to Go, Rust.)


Memory align (for struct)

Rules:

  • Before each individual member, there will be padding so that to make it start at an address that is divisible by its alignment requirement.
    E.g., on many systems, an int should start at an address divisible by 4 and a short by 2.
  • char and char[] are special, could be any memory address, so they don't need padding before them.
  • For struct, other than the alignment need for each individual member, the size of whole struct itself will be aligned to a size divisible by strictest alignment requirement of any of its members, by padding at end.
    E.g., on many systems, if struct's largest member is int then by divisible by 4, if short then by 2.

Order of member:

  • The order of member might affect actual size of struct, so take that in mind. E.g., the stu_c and stu_d from example below have the same members, but in different order, and result in different size for the 2 structs.

Address in memory (for struct)

Empty space:

  • Empty space between 2 structs could be used by non-struct variables that could fit in.
    e.g in test_struct_address() below, the variable x resides between adjacent struct g and h.
    No matter whether x is declared, h's address won't change, x just reused the empty space that g wasted.
    Similar case for y.

Example

(for 64 bit system)

memory_align.c:

/**
 * Memory align & padding - for struct.
 * compile: gcc memory_align.c
 * execute: ./a.out
 */ 
#include <stdio.h>

// size is 8, 4 + 1, then round to multiple of 4 (int's size),
struct stu_a {
    int i;
    char c;
};

// size is 16, 8 + 1, then round to multiple of 8 (long's size),
struct stu_b {
    long l;
    char c;
};

// size is 24, l need padding by 4 before it, then round to multiple of 8 (long's size),
struct stu_c {
    int i;
    long l;
    char c;
};

// size is 16, 8 + 4 + 1, then round to multiple of 8 (long's size),
struct stu_d {
    long l;
    int i;
    char c;
};

// size is 16, 8 + 4 + 1, then round to multiple of 8 (double's size),
struct stu_e {
    double d;
    int i;
    char c;
};

// size is 24, d need align to 8, then round to multiple of 8 (double's size),
struct stu_f {
    int i;
    double d;
    char c;
};

// size is 4,
struct stu_g {
    int i;
};

// size is 8,
struct stu_h {
    long l;
};

// test - padding within a single struct,
int test_struct_padding() {
    printf("%s: %ld\n", "stu_a", sizeof(struct stu_a));
    printf("%s: %ld\n", "stu_b", sizeof(struct stu_b));
    printf("%s: %ld\n", "stu_c", sizeof(struct stu_c));
    printf("%s: %ld\n", "stu_d", sizeof(struct stu_d));
    printf("%s: %ld\n", "stu_e", sizeof(struct stu_e));
    printf("%s: %ld\n", "stu_f", sizeof(struct stu_f));

    printf("%s: %ld\n", "stu_g", sizeof(struct stu_g));
    printf("%s: %ld\n", "stu_h", sizeof(struct stu_h));

    return 0;
}

// test - address of struct,
int test_struct_address() {
    printf("%s: %ld\n", "stu_g", sizeof(struct stu_g));
    printf("%s: %ld\n", "stu_h", sizeof(struct stu_h));
    printf("%s: %ld\n", "stu_f", sizeof(struct stu_f));

    struct stu_g g;
    struct stu_h h;
    struct stu_f f1;
    struct stu_f f2;
    int x = 1;
    long y = 1;

    printf("address of %s: %p\n", "g", &g);
    printf("address of %s: %p\n", "h", &h);
    printf("address of %s: %p\n", "f1", &f1);
    printf("address of %s: %p\n", "f2", &f2);
    printf("address of %s: %p\n", "x", &x);
    printf("address of %s: %p\n", "y", &y);

    // g is only 4 bytes itself, but distance to next struct is 16 bytes(on 64 bit system) or 8 bytes(on 32 bit system),
    printf("space between %s and %s: %ld\n", "g", "h", (long)(&h) - (long)(&g));

    // h is only 8 bytes itself, but distance to next struct is 16 bytes(on 64 bit system) or 8 bytes(on 32 bit system),
    printf("space between %s and %s: %ld\n", "h", "f1", (long)(&f1) - (long)(&h));

    // f1 is only 24 bytes itself, but distance to next struct is 32 bytes(on 64 bit system) or 24 bytes(on 32 bit system),
    printf("space between %s and %s: %ld\n", "f1", "f2", (long)(&f2) - (long)(&f1));

    // x is not a struct, and it reuse those empty space between struts, which exists due to padding, e.g between g & h,
    printf("space between %s and %s: %ld\n", "x", "f2", (long)(&x) - (long)(&f2));
    printf("space between %s and %s: %ld\n", "g", "x", (long)(&x) - (long)(&g));

    // y is not a struct, and it reuse those empty space between struts, which exists due to padding, e.g between h & f1,
    printf("space between %s and %s: %ld\n", "x", "y", (long)(&y) - (long)(&x));
    printf("space between %s and %s: %ld\n", "h", "y", (long)(&y) - (long)(&h));

    return 0;
}

int main(int argc, char * argv[]) {
    test_struct_padding();
    // test_struct_address();

    return 0;
}

Execution result - test_struct_padding():

stu_a: 8
stu_b: 16
stu_c: 24
stu_d: 16
stu_e: 16
stu_f: 24
stu_g: 4
stu_h: 8

Execution result - test_struct_address():

stu_g: 4
stu_h: 8
stu_f: 24
address of g: 0x7fffd63a95d0  // struct variable - address dividable by 16,
address of h: 0x7fffd63a95e0  // struct variable - address dividable by 16,
address of f1: 0x7fffd63a95f0 // struct variable - address dividable by 16,
address of f2: 0x7fffd63a9610 // struct variable - address dividable by 16,
address of x: 0x7fffd63a95dc  // non-struct variable - resides within the empty space between struct variable g & h.
address of y: 0x7fffd63a95e8  // non-struct variable - resides within the empty space between struct variable h & f1.
space between g and h: 16
space between h and f1: 16
space between f1 and f2: 32
space between x and f2: -52
space between g and x: 12
space between x and y: 12
space between h and y: 8

Thus address start for each variable is g:d0 x:dc h:e0 y:e8

๐ŸŒ
Fresh2Refresh
fresh2refresh.com โ€บ home โ€บ c programming tutorial โ€บ c โ€“ structure padding
Structure padding in C
September 22, 2020 - In order to align the data in memory, one or more empty bytes (addresses) are inserted (or left empty) between memory addresses which are allocated for other structure members while memory allocation. This concept is called structure padding.
๐ŸŒ
Medium
medium.com โ€บ mycsdegree โ€บ struct-padding-in-c-overview-examples-visuals-96888cae82fe
Struct Padding in C: Overview, Examples, Visuals | by Kyra Krishna | mycsdegree | Medium
February 16, 2023 - At first glance, you would expect ... the sizeof(student) to be 32 instead. ... Well, C uses something called struct padding to align data ......
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ c_structs_padding.php
C Struct Alignment and Padding
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.
Find elsewhere
๐ŸŒ
LinkedIn
linkedin.com โ€บ pulse โ€บ structure-padding-uttam-basu
Structure Padding
September 22, 2023 - In C, structure padding (also known as data structure padding or struct padding) is a technique used by the compiler to optimize memory access and alignment of data members within a structure.
๐ŸŒ
Aticleworld
aticleworld.com โ€บ home โ€บ understanding of structure padding in c with alignment
Understanding of structure padding in C with alignment - Aticleworld
July 17, 2022 - When you create an object of structure or union the compiler may insert some extra bytes between the members of the structure or union for the alignment. These extra unused bytes are called padding bytes and this technique is called structure ...
๐ŸŒ
Javatpoint
javatpoint.com โ€บ structure-padding-in-c
Structure Padding in C - javatpoint
March 1, 2022 - Structure Padding in C with Tutorial, C language with programming examples for beginners and professionals covering concepts, c array, c pointers, c structures, c union, c strings etc.
๐ŸŒ
Reddit
reddit.com โ€บ r/programming โ€บ how structure-padding in c/c++ actually works
r/programming on Reddit: How structure-padding in C/C++ actually works
May 13, 2024 - What might be pragma pack in one compiler might be pragma unpadded in another. You should also perhaps mention that placing the largest elements first in the struct, then the next largest, and so on ... wastes the least space, because each field is aligned. So interspersing 1-byte fields between 8-byte fields leaves tons of padding after the 1-byte field and before the next 8-byte field.
๐ŸŒ
EDUCBA
educba.com โ€บ home โ€บ software development โ€บ software development tutorials โ€บ c programming tutorial โ€บ structure padding in c
Structure Padding in C | How Does Structure Padding Work in C?
February 16, 2023 - Structure padding mainly talks about memory for variables which are aligned based on the size of the variable. Let suppose a โ€œcharโ€ of 1 byte memory can be assigned anywhere in between like 0x5000 to 0x5001.
Call ย  +917738666252
Address ย  Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
๐ŸŒ
Edureka
edureka.co โ€บ blog โ€บ understanding-structures-and-padding-in-c
Understanding Structures and Padding in C | Edureka.co
February 1, 2021 - And an โ€˜intโ€™ of 4 bytes, must start at a 4-byte boundary like 0x5004 or 0x5008. The structure padding is automatically done by the compiler to make sure all its members are byte aligned.
๐ŸŒ
EmbeTronicX
embetronicx.com โ€บ tutorials โ€บ p_language โ€บ c โ€บ structures-in-c-programming
Structure, Structure Padding, Packing, Bit fields in C โ‹† EmbeTronicX
October 28, 2023 - It has to read the first 4 bytes from the memory address 0x00000000 in one memory cycle and then read the memory address 0x00000004 in the second memory cycle. And it has to do some bit shifting also. If the processor or controller does this for multiple misaligned data, then it will take more time to process. Effectively that means it will take at least two times as long as it would if the data were properly aligned. For this reason, computer scientists came up with the idea of adding padding to the data memory.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ how-to-avoid-structure-padding-in-c
How to avoid Structure Padding in C? - GeeksforGeeks
July 11, 2025 - Prerequisites: Structure Member Alignment, Padding and Data Packing In Structure, sometimes the size of the structure is more than the size of all structures members because of structure padding. Below is an example of Structure padding: ... // C program to show an example // of Structure padding #include <stdio.h> struct s { int i; char ch; double d; }; int main() { struct s A; printf("Size of A is: %ld", sizeof(A)); }
๐ŸŒ
Quora
quora.com โ€บ What-is-structure-padding-in-C
What is structure padding in C? - Quora
Answer (1 of 6): Let us consider two simple examples to understand the concept clearly. Example 1: [code]struct A { char c; char ch; }; int main() { printf("%d bytes", sizeof(struct A)); prints 2 bytes return 0; } [/code]In the above example, each char variables is one byte. So, without an...
๐ŸŒ
Reddit
reddit.com โ€บ r/c_programming โ€บ is struct padding defined in any c standard?
r/C_Programming on Reddit: Is struct padding defined in any C standard?
August 6, 2019 -

So in my project I have types of each of struct fields and I need to construct proper C structs out of them at runtime. I can't change struct definitions to disable padding, because they are not a part of the project. Currently I'm using libffi to calculate the padding for me, but ideally I'd like to not have this dependency.

I know about rules regarding padding elements in C structs, but I'm not sure if they work same way on every compiler. I would really appreciate a link to the part of C standard that defines this behavior.

๐ŸŒ
Medium
medium.com โ€บ @geeky.vartika โ€บ padding-in-c-structures-a-silent-performance-killer-and-how-to-optimize-it-48d212217d61
Padding in C Structures: A Silent Performance Killer (and How to Optimize It) | by Geeky Vartika | Medium
December 31, 2025 - Understanding how and why compilers ... Structure padding refers to unused bytes inserted by the compiler between structure members to satisfy alignment requirements of the target architecture....
๐ŸŒ
Medium
medium.com โ€บ @haadimdwork โ€บ c-struct-padding-and-alignment-85b2011d1f5a
C Struct Padding And Alignment. 1.1 Introduction | by Haadi Mohammed | Medium
February 12, 2025 - So, what the modern-day compiler does is, it performs padding in our struct to ensure that the variables would reside at suitable addresses, so that our work is done in minimal memory accesses.