sizeof in C Explained (2026): Master Beginner to Expert Guide With Real Examples

0b63979cd9494aa401d1fce2d73bb002
On: December 8, 2025
sizeof in C

Learn sizeof in C with simple examples. Understand usage, return type, format specifier, array size, strlen vs sizeof, and avoid common mistakes easily.

If you have ever wondered how big a data type, variable, or structure is in memory, you’ve already touched the topic of sizeof in C. It’s one of those things every C programmer uses, yet many misunderstand. So today, let’s sit down, simplify everything, and walk through sizeof from the basics to the tricky parts that show up in interviews.

If you’re learning C programming and want to strengthen your fundamentals before practicing Structures and Unions Interview Questions, you should explore this detailed beginner-friendly guide on structures: Structures in C – Complete Guide
It explains structure syntax, memory layout, padding, nesting, and real-world examples in a very simple way. Reading this will give you a strong foundation before jumping into advanced interview questions.

What is sizeof in C?

The sizeof operator tells you how much memory something takes in bytes.
It works for:

  • data types
  • variables
  • expressions
  • pointers
  • arrays
  • structures
  • unions
  • function return types
  • and more

Example:

printf("%zu", sizeof(int));

This prints the number of bytes needed to store an int on your system.

Why sizeof exists

C runs close to hardware. You often need to know how much memory something consumes to:

  • allocate memory safely
  • handle arrays
  • write low-level drivers
  • work with network packets
  • optimize memory usage

Without sizeof, developers would guess memory sizes. And guessing in C is dangerous.

How to use sizeof in C

The syntax is simple.

sizeof(type)
sizeof variable
sizeof expression

Examples:

sizeof(int)
sizeof x
sizeof(x+10)

All are valid.

Syntax and examples

int a = 10;
printf("%zu", sizeof(a));     // sizeof variable
printf("%zu", sizeof(int));   // sizeof type
printf("%zu", sizeof(a+1));   // sizeof expression

What does sizeof in C return?

sizeof returns the size in bytes.

If int is 4 bytes:

sizeof(int) → 4

If a structure is 16 bytes:

sizeof(struct Example) → 16

Return type of sizeof in C

The return type of sizeof in C is:

size_t

size_t is an unsigned integer type used for sizes.
It’s defined in .

Is sizeof a function in C?

No.

sizeof is an operator, not a function.

This is important because:

  • it is evaluated at compile time (for most cases)
  • it does not cause side effects
  • it doesn’t evaluate expressions fully

Example:

sizeof(a++);

a will NOT increment.

What library is sizeof in C?

No library.
sizeof is built directly into the compiler.

You do not need to include any header.

Format specifier for sizeof in C

Since sizeof returns a size_t, you print it using:

%zu

Example:

printf("%zu", sizeof(int));

Many programmers mistakenly use %d, but %zu is correct.

sizeof with basic data types

This depends on architecture, compiler, alignment, and ABI.

Example on a 64-bit machine:

sizeof(char)  → 1
sizeof(int)   → 4
sizeof(float) → 4
sizeof(double)→ 8
sizeof(long)  → 8

sizeof with variables vs constants

int x = 10;
sizeof x;        // OK
sizeof(10);      // OK: integer constant is type int

sizeof with arrays

This is one of the most important uses.

int arr[10];
sizeof(arr); // 40 bytes (if int=4)

General rule:

sizeof(array) = element_size × number_of_elements

But note:

sizeof(arr) != sizeof pointer

More on this later.

sizeof with pointers

int *p;
sizeof(p);     // size of pointer (8 bytes on 64-bit)

Pointers always have the same size regardless of what they point to.

sizeof with strings

Strings are tricky.
Let’s compare:

Case 1: String literal array

char s[] = "Hello";
sizeof(s); 

"Hello" is 5 chars + null char → 6 bytes.

Case 2: Pointer to string literal

char *p = "Hello";
sizeof(p);

This returns pointer size (8 bytes on 64-bit), NOT 6.

strlen vs sizeof in C

One of the most asked questions.

strlen

  • counts characters in a string
  • stops at NULL '\0'
  • works at run time

sizeof

  • gives total memory size
  • works at compile time
  • includes '\0' in string arrays

difference between strlen and sizeof in C

Let’s compare them clearly:

ExpressionResultWhy
sizeof("Hello")6includes null terminator
strlen("Hello")5counts until null but not including it

Example:

char s[] = "Hello";
printf("%zu", sizeof(s));   // 6
printf("%zu", strlen(s));   // 5

sizeof with functions

You cannot do:

sizeof(func)

But you can do:

sizeof(func())  // returns size of return type

Example:

int fun();
sizeof(fun());   // size of int

sizeof with structures

sizeof accounts for:

  • actual member sizes
  • padding for alignment

Example:

struct A {
    char a;    // 1 byte
    int b;     // 4 bytes
};
sizeof(struct A);

Most compilers will align the struct → 8 bytes.

sizeof with unions

Unions store all members in the same memory location.

Rule:

sizeof(union) = size of largest member

Example:

union B {
    int x;        // 4 bytes
    double y;     // 8 bytes
};
sizeof(union B); // 8

sizeof with enums

Enums are generally the size of an int:

sizeof(enum Example) → 4

sizeof with typedef

typedef does not affect memory layout.

Example:

typedef int myInt;
sizeof(myInt) = sizeof(int);

sizeof and operator precedence

Parentheses are optional for variables:

sizeof x;       // OK

But required for types:

sizeof(int);    // parentheses required

sizeof and expressions

sizeof does not evaluate the expression inside.

Example:

int x = 10;
sizeof(x++);   // x does not increment

The compiler only checks the type of x++.

sizeof and macros

Common macro to find array length:

#define ARRAY_LEN(a)  (sizeof(a) / sizeof(a[0]))

implementation of sizeof in C (internal view)

sizeof is implemented by the compiler:

  • It does not generate assembly instructions.
  • It is evaluated during compilation.
  • It uses type information from symbol tables.

When applied to variable length arrays, compile-time evaluation is not possible; then sizeof becomes runtime.

Simple Way to “Implement sizeof” in C (Conceptual Trick)

In C, you cannot really implement sizeof because it is a compiler operator.

But you can simulate it for known types using a simple pointer trick.

#define my_sizeof(type) ((char *)(&type + 1) - (char *)(&type))

How this works?

  • ( &type ) → address of the variable
  • ( &type + 1 ) → address of next element after the variable
  • Casting both to char* means pointer moves 1 byte at a time
  • Subtracting addresses gives number of bytes occupied

Example Usage

#include 

#define my_sizeof(type) ((char *)(&type + 1) - (char *)(&type))

int main() {
    int x;
    double d;
    char c;

    printf("Size of int: %ld\n", my_sizeof(x));
    printf("Size of double: %ld\n", my_sizeof(d));
    printf("Size of char: %ld\n", my_sizeof(c));

    return 0;
}

Output (example)

Size of int: 4
Size of double: 8
Size of char: 1

Limitations

  • Works only with variables, not types.
  • Cannot handle:
    • my_sizeof(int) (because no variable)
    • structure padding details with just a type
  • Not equivalent to real sizeof operator.

Bonus Version: For Types Using a Dummy Variable

You can also simulate for types by creating a dummy variable.

#define my_sizeof_type(type) ((size_t)(&((type*)0)[1]) - (size_t)(&((type*)0)[0]))

Usage:

printf("%zu", my_sizeof_type(int));
printf("%zu", my_sizeof_type(double));

This works because:

  • (type*)0 → NULL pointer to type
  • ((type*)0)[1] → next element of array
  • Subtraction gives size of type
  • Real sizeof is done by the compiler, not in C code
  • Above macros simulate the logic using pointer arithmetic
  • They are ONLY for learning purpose

sizeof portability issues

Avoid assuming specific sizes like:

int = 4 bytes
long = 4 bytes
char = 1 byte

Because:

  • sizes change by architecture
  • padding rules differ
  • compilers treat alignment differently

Always use sizeof instead of hardcoding sizes.

Common pitfalls and mistakes

Mistake 1: Using sizeof on pointer instead of array

int *p = malloc(10 * sizeof(int));
sizeof(p); // wrong: gives pointer size

Mistake 2: Using %d instead of %zu for printing sizeof

Mistake 3: Using strlen to measure binary data

strlen stops at '\0', sizeof doesn’t.

Interview Questions (Beginner to Advanced)

  1. What is sizeof in C?
  2. Is sizeof a function or operator?
  3. What is the return type of sizeof?
  4. Does sizeof evaluate expressions?
  5. How do you print sizeof results?
  6. What is the format specifier for sizeof in C?
  7. How to use sizeof with arrays?
  8. Why sizeof pointer != sizeof array?
  9. strlen vs sizeof in C — explain with examples.
  10. difference between strlen and sizeof in C
  11. What does sizeof in C return for a struct?
  12. How does padding affect struct size?
  13. sizeof with string literal vs char pointer
  14. sizeof with union — why max member?
  15. Can sizeof be overloaded?
  16. Can sizeof work on functions?
  17. How does compiler implement sizeof?
  18. sizeof for VLA (Variable Length Arrays)
  19. Why sizeof returns size_t?
  20. What library is sizeof in C?

These are commonly asked in interviews for freshers, embedded developers, and experienced programmers.

Summary

Here’s what you should remember:

  • sizeof in C is a compile-time operator.
  • It returns size in bytes of any type or object.
  • The return type is size_t.
  • Use %zu to print it.
  • sizeof(array) gives full array size; sizeof(pointer) does not.
  • strlen counts characters; sizeof counts memory.
  • sizeof never executes expressions inside it.
  • Struct size includes padding.
  • Union size is maximum member size.
  • sizeof is defined by the compiler, not in a library.

FAQ

1. What is sizeof in C used for?

It tells you how much memory a data type or variable takes in bytes. It’s mostly used for memory allocation and array calculations.

2. Is sizeof a function or an operator?

It is an operator, not a function.

3. How do I print sizeof in C?

Use the correct format specifier:

printf("%zu", sizeof(int));

4. What is the return type of sizeof in C?

size_t, which is an unsigned type used for sizes.

5. How to use sizeof in C with arrays?

int arr[10];
printf("%zu", sizeof(arr));   // 40 bytes if int = 4

6. What does sizeof return for pointers?

Always the size of the pointer itself, not the memory it points to.

7. What library defines sizeof?

None. sizeof is built into the compiler.

8. difference between strlen and sizeof in C?

strlen counts characters; sizeof counts memory.

9. How to use sizeof safely in dynamic memory?

Always use:

malloc(n * sizeof(*ptr));

10. Can sizeof change between systems?

Yes, sizes of int, long, pointers vary by architecture. Always use sizeof instead of hardcoding size values.

Leave a Comment