Need Help or More Information?
Questions about this free C programming course, embedded C training or career guidance? Get in touch.
C sizeof Operator Explained
Learn the C sizeof operator, array counting, struct padding and fixed-width types, plus a bitwise preview, in our free C programming course.
Welcome to Lecture 11 of our free C programming course. Today you meet sizeof, the operator that answers a question every firmware engineer asks daily: how many bytes does this occupy? It is the foundation of portable embedded C programming, because the size of int, long and pointers changes from one chip to another.
This lesson is part of a free C tutorial for C programming for embedded systems. At the end you also get a short preview of the bitwise operators, which the next lectures cover in depth. All programs were compiled with GCC 13 (-Wall -Wextra -std=c11) on a 64-bit Linux PC and executed, so the numbers are real for that platform.
What You Will Learn in This Free C Programming Course Lesson
- What
sizeofreturns and why its type issize_t - The two syntax forms:
sizeof(type)andsizeof expression - Why
sizeof(int)is not always 2 (or 4) - Why
sizeof(i++)never incrementsi - How to count array elements, and the trap inside functions
- Struct padding, and why members add up to less than the struct size
- A quick preview of the six bitwise operators
Prerequisites
You should know C data types (char, int, float, double), constants, arrays at a basic level, and printf. This free embedded C course assumes you have followed the previous lectures in Chapter 4.
What the C sizeof Operator Does
sizeof is a unary operator. It gives the size of its operand in bytes. The operand can be a variable, a constant, an expression, or a data type such as int, float or char. The result has the unsigned type size_t, so print it with the %zu format specifier.
| Form | Example | Parentheses? |
|---|---|---|
| Size of a type | sizeof(int) | Required |
| Size of an object | sizeof var or sizeof(var) | Optional |
| Size of an expression | sizeof (a + b) | Needed for grouping |
A Note on Older Books
Older textbooks written for 16-bit compilers say sizeof(int) is 2 bytes. On a modern PC or a 32-bit ARM Cortex-M the answer is 4. On small 8-bit and 16-bit microcontrollers (some AVR and MSP430 compilers) int is still 2 bytes. The C standard only promises minimum ranges, which is exactly why sizeof exists.
Sizes of the Basic Types on This Machine
Run this on your own computer and compare. On a typical 64-bit Linux PC with GCC the output is:
#include <stdio.h>
int main(void)
{
printf("char : %zu\n", sizeof(char));
printf("short : %zu\n", sizeof(short));
printf("int : %zu\n", sizeof(int));
printf("long : %zu\n", sizeof(long));
printf("long long : %zu\n", sizeof(long long));
printf("float : %zu\n", sizeof(float));
printf("double : %zu\n", sizeof(double));
printf("long double : %zu\n", sizeof(long double));
printf("void * : %zu\n", sizeof(void *));
return 0;
}
Build and run it:
gcc -Wall -Wextra -std=c11 -o ep_sz_types ep_sz_types.c
./ep_sz_types
Expected output:
char : 1
short : 2
int : 4
long : 8
long long : 8
float : 4
double : 8
long double : 16
void * : 8
| Type | 64-bit Linux PC (measured above) | Typical 32-bit ARM Cortex-M (not run here) | Typical 8-bit AVR (not run here) |
|---|---|---|---|
char | 1 | 1 | 1 |
short | 2 | 2 | 2 |
int | 4 | 4 | 2 |
long | 8 | 4 | 4 |
void * | 8 | 4 | 2 |
The first column was measured by the program above. The other two columns show commonly seen values for those platforms; confirm them with your own toolchain by compiling the same program for your target. The one fixed rule: sizeof(char) is always 1.
Byte cells occupied by each type on the PC above
sizeof on Constants and Expressions
What Does sizeof Say About Literals?
Constants have types too. An unsuffixed integer constant is an int, a suffix L makes it long, a decimal point makes it double, and an f suffix makes it float. The program also shows two surprises: a character constant and a string literal.
#include <stdio.h>
int main(void)
{
int var = 0, i = 5;
printf("sizeof var = %zu\n", sizeof var);
printf("sizeof(var) = %zu\n", sizeof(var));
printf("sizeof 45 = %zu\n", sizeof 45);
printf("sizeof 45L = %zu\n", sizeof 45L);
printf("sizeof 3.14 = %zu\n", sizeof 3.14);
printf("sizeof 3.14f = %zu\n", sizeof 3.14f);
printf("sizeof 'A' = %zu\n", sizeof 'A');
printf("sizeof \"hello\" = %zu\n", sizeof "hello");
printf("\nbefore: i = %d\n", i);
printf("sizeof(i++) = %zu\n", sizeof(i++));
printf("after : i = %d (i++ was never executed)\n", i);
return 0;
}
Build and run it:
gcc -Wall -Wextra -std=c11 -o ep_sz_expr ep_sz_expr.c
./ep_sz_expr
Expected output:
sizeof var = 4
sizeof(var) = 4
sizeof 45 = 4
sizeof 45L = 8
sizeof 3.14 = 8
sizeof 3.14f = 4
sizeof 'A' = 4
sizeof "hello" = 6
before: i = 5
sizeof(i++) = 4
after : i = 5 (i++ was never executed)
sizeof 'A'is 4 here because in C a character constant has typeint(in C++ it would be 1).sizeof "hello"is 6: five letters plus the terminating'\0'.sizeof(i++)gave 4 andistayed 5. The operand ofsizeofis not evaluated; the compiler only looks at its type.
sizeof is answered at compile time
No Side Effects Inside sizeof
Never rely on a side effect inside sizeof. Writing sizeof(count++) does not count anything. The only exception is variable-length arrays (VLAs), where the size is computed at run time; that corner is rare in embedded code.
Counting Array Elements with sizeof
The standard idiom divides the size of the whole array by the size of one element. It adapts automatically when you change the array type or length:
The Array Count Idiom and Its Trap
Inside main the array is a real array, so the arithmetic works. Inside bad_count() the parameter looks like an array but is really a pointer, so sizeof gives the pointer size (8 bytes here). GCC even warns you:
#include <stdio.h>
static void bad_count(int arr[])
{
printf("inside function : sizeof arr = %zu\n", sizeof arr);
}
int main(void)
{
int data[10] = {0};
size_t n = sizeof data / sizeof data[0];
printf("sizeof data = %zu bytes\n", sizeof data);
printf("sizeof data[0] = %zu bytes\n", sizeof data[0]);
printf("element count = %zu\n", n);
bad_count(data);
return 0;
}
Build and run it:
gcc -Wall -Wextra -std=c11 -o ep_sz_array ep_sz_array.c
./ep_sz_array
Expected output:
sizeof data = 40 bytes
sizeof data[0] = 4 bytes
element count = 10
inside function : sizeof arr = 8
Why the count breaks inside a function
The fix is simple: pass the element count as a separate parameter, just like the standard library does with memcpy(dst, src, n).
Embedded C Programming Example: Struct Padding
A struct is not always the sum of its members. The compiler inserts unused padding bytes so each member sits at an address that suits the CPU. Padding matters in embedded work when you map a struct onto a hardware register block, a network packet or a flash record.
Same Members, Different Sizes
Both structs hold one int and two char values, but their size differs because of the order of the members:
#include <stdio.h>
struct loose { char tag; int value; char flag; };
struct packed_order { int value; char tag; char flag; };
int main(void)
{
printf("sizeof(struct loose) = %zu\n", sizeof(struct loose));
printf("sizeof(struct packed_order) = %zu\n", sizeof(struct packed_order));
printf("members add up to = %zu\n", sizeof(char) + sizeof(int) + sizeof(char));
return 0;
}
Build and run it:
gcc -Wall -Wextra -std=c11 -o ep_sz_struct ep_sz_struct.c
./ep_sz_struct
Expected output:
sizeof(struct loose) = 12
sizeof(struct packed_order) = 8
members add up to = 6
Memory layout of struct loose (12 bytes)
Memory layout of struct packed_order (8 bytes)
Putting the largest member first and grouping small members together removes most padding. If a hardware layout forces a different order, GCC offers __attribute__((packed)), but packed structs can cause slow or even faulting unaligned accesses on some CPUs, so use it with care.
Fixed-Width Types for Portable Firmware
When a register is exactly 16 bits wide, do not hope unsigned int is 16 bits. Include <stdint.h> and use uint8_t, uint16_t, uint32_t and uint64_t, whose sizes are guaranteed.
Fixed-Width Integer Sizes
This output is the same on every platform that provides these types:
#include <stdio.h>
#include <stdint.h>
int main(void)
{
printf("uint8_t : %zu\n", sizeof(uint8_t));
printf("uint16_t : %zu\n", sizeof(uint16_t));
printf("uint32_t : %zu\n", sizeof(uint32_t));
printf("uint64_t : %zu\n", sizeof(uint64_t));
return 0;
}
Build and run it:
gcc -Wall -Wextra -std=c11 -o ep_sz_fixed ep_sz_fixed.c
./ep_sz_fixed
Expected output:
uint8_t : 1
uint16_t : 2
uint32_t : 4
uint64_t : 8
Why sizeof Makes Portable Code
If you assume int is 2 bytes and move to a platform where it is 4 bytes, your program may break. Writing malloc(n * sizeof(int)) or sizeof buffer instead of a hard-coded number lets the same source work on every machine. Prefer sizeof *ptr to sizeof(type) when allocating: p = malloc(n * sizeof *p); stays correct even if you later change the type of p.
Preview: Bitwise Operators
C can manipulate data at the level of individual bits, which is vital for registers, flags and masks in embedded systems. Bitwise operators work on integers only. Here is the list; each gets a full lecture later in this free embedded C course.
| Operator | Meaning |
|---|---|
& | bitwise AND |
| | bitwise OR |
~ | one’s complement (flip every bit) |
<< | left shift |
>> | right shift |
^ | bitwise XOR |
A Quick Taste with 12 and 10
Take a = 12 (binary 1100) and b = 10 (binary 1010). Work out each result on paper first, then check:
#include <stdio.h>
int main(void)
{
unsigned a = 12, b = 10; /* 1100 and 1010 */
printf("a & b = %u\n", a & b);
printf("a | b = %u\n", a | b);
printf("a ^ b = %u\n", a ^ b);
printf("a << 1 = %u\n", a << 1);
printf("a >> 2 = %u\n", a >> 2);
printf("~a & 0xF = %u\n", ~a & 0xFu);
return 0;
}
Build and run it:
gcc -Wall -Wextra -std=c11 -o ep_bits_peek ep_bits_peek.c
./ep_bits_peek
Expected output:
a & b = 8
a | b = 14
a ^ b = 6
a << 1 = 24
a >> 2 = 3
~a & 0xF = 3
12 & 10 bit by bit
The last line, ~a & 0xF, flips the four low bits of 12 (1100 becomes 0011) and masks off the rest, giving 3. Before the type cast operator, which closes Chapter 4, you will study type conversions in C.
Common Mistakes with sizeof
- Using sizeof on an array parameter: you get the pointer size, not the array size.
- Printing with %d:
sizeofreturnssize_t; use%zu, or cast tointfor old compilers. - Expecting side effects:
sizeof(i++)never changesi. - Adding up member sizes for a struct: padding makes the real size larger.
- Assuming sizeof(int) == 2 or 4: always ask the compiler.
- sizeof on a pointer to a string: for
char *s = "hello",sizeof sis the pointer size, while forchar s[] = "hello"it is 6.
Best Practices
- Use
sizeofinstead of hard-coded byte counts, always. - Use
sizeof *pwhen allocating memory for a pointerp. - Use
sizeof arr / sizeof arr[0]only wherearris a true array, and wrap it in a macro namedARRAY_LENif you use it often. - Use
<stdint.h>fixed-width types for registers and protocols. - Order struct members from largest to smallest to save RAM on small chips.
- Print sizes with
%zu.
Security and Memory Notes
Wrong size arguments to functions such as memcpy and malloc are a classic source of buffer overflows. Letting the compiler compute the size with sizeof removes a whole class of mistakes. Be careful when copying structs between systems: padding bytes can leak uninitialised memory contents into network packets or files.
Summary: Key Takeaways from This Free C Programming Course Lesson
| Topic | Remember |
|---|---|
| Result | Size in bytes, type size_t, print with %zu |
| When | Computed at compile time; the operand is not evaluated |
| Types | Parentheses required for type names |
| Arrays | sizeof arr / sizeof arr[0] counts elements, but not through a function parameter |
| Structs | Size includes padding |
| Portability | Never hard-code sizes; use sizeof and stdint.h |
Conclusion
You have now covered arithmetic, relational, logical, conditional, comma and sizeof operators. That is nearly the whole operator toolbox of C. In the next lectures of this free C programming course we explore type conversions and the cast operator, then dive deep into bitwise operators, the heart of register-level embedded C programming.
Quiz: Test Your Understanding
Question 1. What is sizeof(char) on every C implementation?
Show answer
Always 1. All other sizes are measured in units of char.
Question 2. After int i = 5; size_t s = sizeof(i++); what is i?
Show answer
5. The operand of sizeof is not evaluated, only its type is examined.
Question 3. Inside void f(int arr[]), what does sizeof arr give?
Show answer
The size of a pointer (for example 8 on a 64-bit PC, 4 on a 32-bit ARM). The array parameter has decayed to int *.
Question 4. What is sizeof "hello"?
Show answer
6, because the terminating null character is included.
Question 5. What printf specifier prints a sizeof result without a cast?
Show answer
%zu.
Question 6. struct { char a; int b; char c; } holds 6 bytes of members. Why is its size usually 12?
Show answer
Padding. The compiler aligns b to a 4-byte boundary and pads the end so arrays of the struct stay aligned.
Interview Questions: Free C Programming Course Edition
Interview Question 1. Is sizeof a function?
Show answer
No, it is an operator evaluated by the compiler. That is why sizeof x works without parentheses.
Interview Question 2. What is the type of the result of sizeof?
Show answer
size_t, an unsigned integer type defined in <stddef.h> and other headers.
Interview Question 3. Why is sizeof(int) not fixed?
Show answer
The C standard only guarantees minimum ranges. The real size depends on the compiler and CPU architecture.
Interview Question 4. How do you compute the number of elements in an array?
Show answer
sizeof arr / sizeof arr[0], valid only where arr is an actual array, not a pointer or array parameter.
Interview Question 5. What is structure padding and how do you reduce it?
Show answer
Padding is unused bytes the compiler inserts to align members. Reorder members from largest to smallest, or use a packed attribute with care.
Interview Question 6. Why prefer malloc(n * sizeof *p) over malloc(n * sizeof(int))?
Show answer
If the type of p changes later, the allocation size stays correct automatically.
Frequently Asked Questions
What does sizeof return in C?
It returns the size of its operand in bytes as a value of type size_t. The operand can be a type, a variable, a constant or an expression.
Is sizeof(int) always 4 bytes?
No. It is commonly 4 on PCs and 32-bit microcontrollers, but 2 on some 8-bit and 16-bit targets. Always use sizeof instead of assuming.
Is sizeof evaluated at run time?
Normally no. The compiler computes it at compile time and the operand is not evaluated, except for variable-length arrays.
How do I get the number of elements in a C array?
Use sizeof array / sizeof array[0]. It works only for real arrays, not for array parameters that have become pointers.
Why is sizeof of a struct larger than its members combined?
The compiler inserts padding bytes between and after members to meet alignment rules.
Which printf format should I use for sizeof?
Use %zu, the format specifier for size_t.
Is this sizeof tutorial part of a free course?
Yes. It is part of the free C programming course on EmbeddedPathashala for students learning embedded C programming.
Continue the Free C Programming Course
Next up: type conversions and the cast operator. Run today’s size table on your own board’s toolchain and compare the numbers.
Need Help or More Information?
Questions about this free C programming course, embedded C training or career guidance? Get in touch.

2 Comments