C Structures and Padding

Beginner
8 min read
Updated: Nov 2026
3 examples

What Is a Structure in C?

A structure (struct) groups named members of different types under one name. The compiler lays members out in order and may insert unused padding bytes so each member is correctly aligned. Padding is why sizeof(struct) can be larger than the sum of its members, and why comparing structs with memcmp() is risky.

Remember
struct Item {
    char tag;     /* offset 0, 1 byte               */
                  /* offsets 1-3: padding (typical) */
    int  value;   /* offset 4, 4 bytes              */
};                /* sizeof(struct Item) is 8       */

member order and alignment decide the layout
padding bytes hold unspecified values
ConceptMeaningTool
MemberA named field inside the structs.field / p->field
AlignmentAddress rule for a type (often its size)_Alignof
PaddingUnused bytes inserted for alignmentoffsetof, sizeof
Struct sizeIncludes all padding, even at the endsizeof

How to Use Structures in C

Declare the type once, create variables of it, and read or write members with the dot or arrow operator.

  1. Declare the type: struct Item { char tag; int value; };.
  2. Create a variable: struct Item a = { 'A', 10 };.
  3. Access members with a.value, or p->value through a pointer.
  4. Inspect layout with sizeof and offsetof from <stddef.h>.
  5. Compare structs member by member, not with memcmp() (unless every byte is controlled).
  6. Compile with gcc file.c -std=c11 -o out and run.
SyntaxUsed forExample
s.mMember of a struct variablea.value
p->mMember through a pointerpa->value
offsetof(T, m)Byte offset of a memberoffsetof(struct Item, value)
sizeof(T)Total size with paddingsizeof(struct Item)

Pseudocode

Pseudocode
declare struct type with members
create variables and set members

to inspect layout:
    sizeof(struct)         total bytes
    offsetof(struct, m)    where member m starts

to compare two structs:
    compare each member with == (or strcmp for strings)
    do NOT rely on memcmp, padding bytes may differ

Cheat Sheet

Layout and comparison rules worth remembering.

TopicDetail
Member orderMemory layout follows declaration order
Padding betweenInserted so the next member meets its alignment
Trailing paddingAdded so arrays of the struct stay aligned
Smaller layoutList larger members first to reduce padding
Padding valuesUnspecified, can differ between equal structs
Safe equalityCompare members individually

Worked Walkthrough — Reordering Members

Assume sizeof(int) is 4 and int needs 4-byte alignment (typical on common platforms).

Calculation
struct A { char a; int b; char c; };

  a: offset 0   (1 byte)
     offsets 1-3 padding
  b: offset 4   (4 bytes)
  c: offset 8   (1 byte)
     offsets 9-11 trailing padding
  sizeof(struct A) = 12

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

  b: offset 0   (4 bytes)
  a: offset 4   (1 byte)
  c: offset 5   (1 byte)
     offsets 6-7 trailing padding
  sizeof(struct B) = 8

same members, different order, 4 bytes saved

C Examples

Compile with gcc file.c -std=c11 -o out. Layout numbers below assume a 4-byte int, as on typical x86-64 and ARM64 systems. Related: memcpy() and memset().

Example 1 — Declare and Print a Struct

A small struct with two members, initialized and read with the dot operator.

C
#include <stdio.h>
      
      struct Item {
          char tag;
          int value;
      };
      
      int main(void) {
          struct Item a = { 'A', 10 };
          struct Item *p = &a;
      
          printf("tag = %c, value = %d\n", a.tag, a.value);
          p->value = 25;
          printf("after update: value = %d\n", a.value);
          return 0;
      }

How It Works

  1. Type. struct Item has a char and an int.
  2. Initialize. Members are set in declaration order.
  3. Dot and arrow. a.value and p->value reach the same memory.

Example 2 — Measure Padding with sizeof and offsetof

Two structs with the same members show how order changes size.

C
#include <stddef.h>
      #include <stdio.h>
      
      struct A { char a; int b; char c; };
      struct B { int b; char a; char c; };
      
      int main(void) {
          printf("struct A: size=%zu, a=%zu, b=%zu, c=%zu\n",
                 sizeof(struct A),
                 offsetof(struct A, a),
                 offsetof(struct A, b),
                 offsetof(struct A, c));
          printf("struct B: size=%zu, b=%zu, a=%zu, c=%zu\n",
                 sizeof(struct B),
                 offsetof(struct B, b),
                 offsetof(struct B, a),
                 offsetof(struct B, c));
          return 0;
      }

How It Works

  1. struct A. The int must start at offset 4, so 3 bytes of padding follow a.
  2. Trailing padding. A also gets 3 bytes at the end, giving size 12.
  3. struct B. Putting the int first needs only 2 trailing bytes, giving size 8.
  4. Padding is real. Those bytes exist in memory but belong to no member.

Example 3 — Compare Structs Member by Member

Because padding bytes are unspecified, compare each member instead of calling memcmp() on the whole struct.

C
#include <stdio.h>
      
      struct Item {
          char tag;
          int value;
      };
      
      static int item_equal(const struct Item *x, const struct Item *y) {
          return x->tag == y->tag && x->value == y->value;
      }
      
      int main(void) {
          struct Item a = { 'A', 10 };
          struct Item b = { 'A', 10 };
          struct Item c = { 'B', 10 };
      
          printf("a == b: %s\n", item_equal(&a, &b) ? "yes" : "no");
          printf("a == c: %s\n", item_equal(&a, &c) ? "yes" : "no");
          return 0;
      }

How It Works

  1. Why not memcmp. The 3 padding bytes after tag may hold different garbage in two equal structs.
  2. Helper function. item_equal compares only real members.
  3. Deterministic result. Equal members give yes regardless of padding contents.
  4. Alternative. memset both to zero first, then fill members, but copies and assignments can still change padding.

Edge Cases & Pitfalls

Struct layout surprises show up in comparisons, files, and networking.

memcmp

memcmp() on whole structs

Padding bytes are unspecified. Two structs with equal members can compare unequal. Compare members, or serialize first.

size

Assuming size is the sum

sizeof includes padding. Do not compute it by adding member sizes.

order

Reordering changes the ABI

Changing member order changes offsets and size, which breaks binary files and shared-library layouts.

files

Writing structs directly to a file

The file then contains padding and platform-specific layout. Write members individually or use a defined format.

packed

Packed structs

Compiler extensions like __attribute__((packed)) remove padding but can cause slow or invalid unaligned access on some CPUs.

Performance Notes

ChoiceEffectBest for
Large members firstLess padding, smaller structBig arrays of structs
Member-wise equalityCorrect, small costAny struct comparison
Packed layoutSmaller, may be slowerWire formats (with care)

Smaller structs fit more per cache line, which can help large arrays. Measure before reordering members for speed, and keep a stable order when the layout is part of a public interface.

Key Takeaways

  • Struct: groups named members of any types.
  • Padding: the compiler adds bytes so members are aligned.
  • Size: sizeof includes padding, including at the end.
  • Equality: compare members, not raw bytes, unless padding is controlled.

One line: a struct is more than its members, so never trust raw-byte comparison.

Frequently Asked Questions

A struct is a user-defined type that groups named members, possibly of different types, into one object.
Extra unused bytes the compiler inserts between or after members so each member meets its alignment requirement.
Padding between members and at the end is part of the struct's size.
It is unsafe. Padding bytes are unspecified, so equal structs may compare unequal. Compare members one by one.
Use offsetof(type, member) from <stddef.h>. It returns a size_t byte offset from the start of the struct.
Order members from largest alignment to smallest, or group same-sized members together.

Next: static_assert

Check struct size and layout at compile time.

static_assert tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful