In the previous topic, we learned how a structure groups different types of data under one name.
Now try a small experiment. Look at this structure:
struct Bad
{
char a; /* 1 byte */
int b; /* 4 bytes */
char c; /* 1 byte */
};
Adding the member sizes gives 1 + 4 + 1 = 6 bytes. But on a typical 64-bit system, sizeof(struct Bad) is 12, not 6.
Where did the extra 6 bytes come from? The answer is structure padding. In this topic we will see why padding happens, how to calculate the size of any structure by hand (a very common interview question), and how bit fields let us go the other way and squeeze several small values into very little memory.
What is Structure Padding?
Structure padding is the unused bytes that the compiler inserts between (or after) structure members so that each member is stored at a properly aligned memory address.
To understand padding, we first need to understand alignment.
What is Alignment?
The processor reads memory most efficiently in fixed-size chunks (for example 4 or 8 bytes at a time). Because of this, most data types have an alignment requirement: their address should be a multiple of a certain number.
A 4-byte
intis usually stored at an address divisible by 4.An 8-byte
doubleis usually stored at an address divisible by 8.A
charcan be stored at any address.
Typical sizes and alignments on a modern 64-bit system (x86-64 or ARM64) are:
Type | Size (bytes) | Alignment (bytes) |
|---|---|---|
| 1 | 1 |
| 2 | 2 |
| 4 | 4 |
| 4 | 4 |
| 8 | 8 |
Any pointer | 8 | 8 |
| 8 on Linux/macOS, 4 on Windows | Same as its size |
These numbers are not fixed by the C standard. They depend on the processor and the compiler. For example, on 32-bit x86 Linux a double inside a structure is only 4-byte aligned. In interviews, always state your assumption, such as "assuming a 4-byte int and 8-byte double".
You can ask the compiler for a type's alignment with the _Alignof operator (C11), also available as alignof through <stdalign.h> in C11 and as a keyword in C23:
printf("%zu\n", _Alignof(double)); /* usually prints 8 */
If a member were placed at a misaligned address, the processor might need two memory reads instead of one, and on some processors a misaligned access even crashes the program. So the compiler quietly adds padding bytes to keep every member aligned.
The Padding Rules
On common compilers (GCC, Clang, MSVC), structure layout follows these simple rules:
Members are stored in the order they are declared. The compiler never reorders them.
The first member is always at offset
0. There is never padding before the first member (this one is guaranteed by the C standard).Each member is placed at the next offset that is a multiple of its alignment. Any skipped bytes are padding.
The total size is rounded up to a multiple of the largest alignment among the members. The extra bytes at the end are called trailing padding.
As formulas:
offset of member = current offset rounded UP to a multiple of its alignment
struct size = end of last member rounded UP to a multiple of
the largest member alignment
The offset of a member is its distance in bytes from the start of the structure. The offsetof macro from <stddef.h> tells us this value.
Worked Example 1: char, int, char
struct Bad
{
char a;
int b;
char c;
};
Let's apply the rules step by step (assuming a 4-byte, 4-aligned int):
Member | Size | Alignment | Next free offset | Placed at | Padding added |
|---|---|---|---|---|---|
| 1 | 1 | 0 | 0 | 0 |
| 4 | 4 | 1 | 4 | 3 (bytes 1 to 3) |
| 1 | 1 | 8 | 8 | 0 |
The last member ends at offset 9. The largest alignment is 4, so 9 is rounded up to 12. That adds 3 bytes of trailing padding.
Offset: 0 1 2 3 4 5 6 7 8 9 10 11
+----+----+----+----+----+----+----+----+----+----+----+----+
| a | P | P | P | b | b | b | b | c | P | P | P |
+----+----+----+----+----+----+----+----+----+----+----+----+
P = padding byte sizeof(struct Bad) = 12
Only 6 of the 12 bytes hold real data. Half the structure is wasted.
Reordering the Members
Now put the large member first:
struct Good
{
int b;
char a;
char c;
};
Offset: 0 1 2 3 4 5 6 7
+----+----+----+----+----+----+----+----+
| b | b | b | b | a | c | P | P |
+----+----+----+----+----+----+----+----+
sizeof(struct Good) = 8
a goes at 4 and c at 5. The data ends at 6, which is rounded up to 8. Same members, 4 bytes saved, just by changing the order.
Let's confirm this with a real program:
#include <stdio.h>
#include <stddef.h>
struct Bad
{
char a;
int b;
char c;
};
struct Good
{
int b;
char a;
char c;
};
int main()
{
printf("struct Bad : size = %zu\n", sizeof(struct Bad));
printf(" a at offset %zu\n", offsetof(struct Bad, a));
printf(" b at offset %zu\n", offsetof(struct Bad, b));
printf(" c at offset %zu\n", offsetof(struct Bad, c));
printf("struct Good: size = %zu\n", sizeof(struct Good));
printf(" b at offset %zu\n", offsetof(struct Good, b));
printf(" a at offset %zu\n", offsetof(struct Good, a));
printf(" c at offset %zu\n", offsetof(struct Good, c));
return 0;
}
Output (on a typical system with a 4-byte int):
struct Bad : size = 12
a at offset 0
b at offset 4
c at offset 8
struct Good: size = 8
b at offset 0
a at offset 4
c at offset 5
As in the Data Types topic, we print sizeof results with %zu. offsetof also gives a size_t value, so it uses %zu too.
Worked Example 2: With a double
struct Record
{
char grade; /* 1 byte */
double salary; /* 8 bytes */
int id; /* 4 bytes */
};
grade→ offset 0 (ends at 1)salaryneeds a multiple of 8 → 7 padding bytes, placed at offset 8 (ends at 16)idneeds a multiple of 4 → 16 is fine, placed at offset 16 (ends at 20)Largest alignment is 8 → 20 rounded up to 24
Real data: 13 bytes. Size: 24 bytes.
Sorted from largest to smallest:
struct RecordSorted
{
double salary; /* offset 0 */
int id; /* offset 8 */
char grade; /* offset 12 */
};
The data ends at 13, rounded up to a multiple of 8 gives 16 bytes. That is a third smaller. In an array of one million records, this saves about 8 MB.
Worked Example 3: Mixed Sizes
struct Mixed
{
char a; /* offset 0 */
short b; /* offset 2 (1 padding byte before) */
char c; /* offset 4 */
int d; /* offset 8 (3 padding bytes before) */
};
d ends at 12, which is already a multiple of 4, so the size is 12 with no trailing padding.
Why Trailing Padding?
Consider:
struct Tail
{
int i; /* offset 0 */
char c; /* offset 4 */
};
The data ends at 5, but sizeof(struct Tail) is 8. The reason is arrays. In an array, elements are stored back to back, so arr[1] starts exactly sizeof(struct Tail) bytes after arr[0]. If the size were 5, arr[1].i would start at address 5, which is not a multiple of 4. Rounding the size up to 8 keeps every element's int aligned.
When There is No Padding
If every member has alignment 1, no padding is needed:
struct Chars
{
char x, y, z;
}; /* sizeof is 3 */
Padding appears only when members with different alignment requirements are mixed.
Packed Structures
Sometimes we need a structure with no padding at all, for example to match the exact byte layout of a hardware register or a file header. Compilers offer non-standard extensions for this:
/* GCC, Clang and MSVC */
#pragma pack(push, 1)
struct Packed
{
char a;
int b;
char c;
};
#pragma pack(pop)
/* GCC and Clang only */
struct Packed2
{
char a;
int b;
char c;
} __attribute__((packed));
Both versions have size 6. But packing has costs:
Accessing misaligned members can be slower, and on some processors the compiler must generate extra instructions.
Taking the address of a misaligned member (for example
int *p = &s.b;) and using it as a normalint *can crash on strict-alignment processors. GCC and Clang warn about this (-Waddress-of-packed-member) for__attribute__((packed))structures, but they do not warn for#pragma packstructures, so do not rely on the warning.It is not portable, since it is not part of standard C.
So the usual advice is: reorder members first, and pack only when you truly need an exact layout.
Bit Fields
Padding wastes memory. Bit fields go the other way: they let us store small values using only as many bits as they need.
Think about a date. A day is between 1 and 31, so it needs only 5 bits (25 = 32 values). A month is between 1 and 12, so it needs only 4 bits (24 = 16 values). Using a full 32-bit int for each is wasteful.
What is a Bit Field?
A bit field is a structure member whose size is given in bits instead of bytes.
The syntax is:
type memberName : width;
where width is the number of bits.
struct Date
{
unsigned int day : 5; /* 0 to 31 */
unsigned int month : 4; /* 0 to 15 */
unsigned int year : 12; /* 0 to 4095 */
};
An unsigned field of n bits can hold values from 0 to 2n - 1.
Width (bits) | Unsigned range | Good for |
|---|---|---|
1 | 0 to 1 | Yes/no flags |
3 | 0 to 7 | Day of the week |
4 | 0 to 15 | Month |
5 | 0 to 31 | Day of the month |
7 | 0 to 127 | Marks out of 100 |
12 | 0 to 4095 | Year |
The three fields of struct Date need 5 + 4 + 12 = 21 bits in total, so they all fit inside one 32-bit unsigned int:
bits: 31 ........ 21 | 20 ........ 9 | 8 .... 5 | 4 .... 0
(unused) | year (12) | month(4) | day (5)
(The exact order of fields inside the storage unit is decided by the compiler. This picture shows the usual layout on x86 with GCC/Clang.)
Complete Example
#include <stdio.h>
struct DateNormal
{
unsigned int day;
unsigned int month;
unsigned int year;
};
struct Date
{
unsigned int day : 5; /* 0 to 31 */
unsigned int month : 4; /* 0 to 15 */
unsigned int year : 12; /* 0 to 4095 */
};
int main()
{
struct Date today = {2, 11, 2026};
printf("Date: %u/%u/%u\n", today.day, today.month, today.year);
printf("sizeof(struct DateNormal) = %zu\n", sizeof(struct DateNormal));
printf("sizeof(struct Date) = %zu\n", sizeof(struct Date));
unsigned int value = 20;
today.month = value; /* 20 does not fit in 4 bits */
printf("month after storing 20 = %u\n", today.month);
return 0;
}
Output (typical, with a 32-bit unsigned int):
Date: 2/11/2026
sizeof(struct DateNormal) = 12
sizeof(struct Date) = 4
month after storing 20 = 4
Two things to notice:
The bit-field version uses 4 bytes instead of 12.
Storing
20in a 4-bit unsigned field silently keeps only the lowest 4 bits. In binary, 20 is10100; the lowest 4 bits are0100, which is 4. In other words, the value is reduced modulo 24 = 16:20 % 16 = 4. No error is reported at runtime, so it is your job to keep values in range.
Bit Fields as Flags
One-bit fields are a readable way to store several yes/no options:
#include <stdio.h>
struct Permissions
{
unsigned int canRead : 1;
unsigned int canWrite : 1;
unsigned int canExecute : 1;
unsigned int isAdmin : 1;
};
int main()
{
struct Permissions p = {0};
p.canRead = 1;
p.canWrite = 1;
printf("read=%u write=%u execute=%u admin=%u\n",
p.canRead, p.canWrite, p.canExecute, p.isAdmin);
printf("sizeof(struct Permissions) = %zu\n", sizeof(struct Permissions));
if (p.canWrite)
{
printf("User can write\n");
}
return 0;
}
Output (typical):
read=1 write=1 execute=0 admin=0
sizeof(struct Permissions) = 4
User can write
Four flags need only 4 bits, but the size is 4 bytes because the fields are stored inside one unsigned int unit. With 32 one-bit fields, the size would still be 4 bytes. The same idea can be done by hand with bitwise operators, which we will cover in the Bitwise Operations topic.
Unnamed and Zero-Width Bit Fields
struct Register
{
unsigned int mode : 3;
unsigned int : 2; /* unnamed: 2 bits of padding, cannot be used */
unsigned int enable : 1;
unsigned int : 0; /* width 0: start the next field in a new unit */
unsigned int status : 4;
};
An unnamed bit field reserves bits that you cannot access. It is used to skip bits, for example to match a hardware register.
A zero-width bit field (it must be unnamed) forces the next bit field to begin in a fresh storage unit. Here
statusstarts in a secondunsigned int, so the size is 8 bytes on typical systems.
Rules and Limitations of Bit Fields
Allowed types: standard C guarantees
int,signed int,unsigned intand_Bool. Many compilers also acceptchar,shortand others as an extension.Prefer
unsigned int. Whether a plainintbit field is signed or unsigned is implementation-defined. A 1-bit signed field can typically hold only0and-1, which surprises many people.No address: you cannot use
&on a bit field, so you cannot have a pointer to one, andscanf("%u", &d.day)will not compile. Read into a normal variable first, then assign.No
sizeofon a bit-field member, and no arrays of bit fields.Width limit: the width cannot be larger than the width of the type (for example, at most 32 for a 32-bit
unsigned int).Layout is implementation-defined: the order of bits, whether a field may cross a unit boundary, and the size of the unit all depend on the compiler. For example,
struct { char c; unsigned int x : 4; }has size 4 with GCC/Clang on Linux but size 8 with MSVC. So never use bit-field structures to read or write a binary file or network packet that must work across compilers.Speed: reading or writing a bit field needs extra shift and mask instructions, so it trades a little speed for memory.
Padding vs Bit Fields
Point | Structure Padding | Bit Fields |
|---|---|---|
Who does it | The compiler, automatically | The programmer, explicitly |
Effect on size | Increases size (adds unused bytes) | Decreases size (packs values into bits) |
Purpose | Fast, correctly aligned memory access | Save memory for small-range values |
Unit | Bytes | Bits |
Address of member | Allowed ( | Not allowed |
Portability | Layout depends on platform | Layout depends on compiler |
Common Mistakes
Assuming
sizeof(struct)equals the sum of member sizes. It is often larger because of padding. Always usesizeof, for examplemalloc(n * sizeof(struct Record)), never a hand-added number.Comparing structures with
memcmp(). Padding bytes can contain garbage, so two structures with equal members may still compare as different. Compare member by member instead. (C does not allow==on structures at all.)Writing a whole structure to a file and reading it on another system. Padding, type sizes and byte order can differ, so the data may be read incorrectly.
Storing an out-of-range value in a bit field and expecting an error. Unsigned bit fields simply keep the low bits.
Using plain
intfor a bit field and getting negative values unexpectedly.Packing structures everywhere to save memory. Reordering members usually gives most of the benefit without the risks.
Interview Tips
"What is sizeof this structure?" is one of the most frequently asked C questions. Use this method every time:
State your assumption (for example,
int= 4,double= 8, 64-bit system).Walk through the members in order, placing each at the next multiple of its alignment.
Round the final offset up to a multiple of the largest alignment.
Try these yourself before opening the answers:
Practice 1: struct { char a; double b; char c; }
a at 0, b at 8 (7 padding bytes), c at 16. Data ends at 17, rounded up to a multiple of 8 gives 24. Reordered as double, char, char, the size becomes 16.
Practice 2: struct { short s; char c; int i; char d; }
s at 0, c at 2, i at 4 (1 padding byte), d at 8. Data ends at 9, rounded up to a multiple of 4 gives 12.
Practice 3: struct { unsigned int a : 3; unsigned int b : 6; unsigned int c : 10; }
3 + 6 + 10 = 19 bits, which fits in one 32-bit unsigned int, so the size is 4 on typical compilers.
Other points interviewers like to hear:
Padding exists for performance and hardware alignment, not by accident.
The standard guarantees no padding before the first member, but padding can appear after any member and at the end.
The simplest way to reduce padding is to order members from largest to smallest alignment.
Bit fields are common in embedded systems, device drivers and protocol headers, but their layout is implementation-defined.
In Simple Words
The compiler adds padding bytes to keep structure members aligned, which can make a structure bigger than the sum of its members. Bit fields let us store small values in just a few bits to make a structure smaller.
Remember:
Each member → placed at a multiple of its alignment
Total size → rounded up to a multiple of the largest alignment
Reduce padding → order members from largest to smallest
Bit field → unsigned int name : bits; (no address, layout depends on compiler)
In the next topic, we will look at unions, where all members share the same memory instead of being laid out one after another.