Back to C Programming
2026-07-125 min read

The sizeof operator (C Programming)

Learn The sizeof operator (C Programming) step by step with clear examples and exercises.

Why This Matters

Understanding the sizeof operator is crucial for efficient C programming as it allows developers to determine the size of data types, variables, or arrays at runtime. By mastering this operator, you'll be better equipped to write optimized code, manage memory effectively, and avoid common pitfalls when working with memory management in C.

Prerequisites

Before diving into the sizeof operator, it is essential that you have a solid understanding of the following concepts:

  • Basic syntax and structure of C programming
  • Data types (int, char, float, etc.)
  • Variables and arrays
  • Pointers (optional but recommended)
  • Basic memory management concepts such as stack and heap memory

Core Concept

The sizeof operator in C returns the size of its operand (in bytes) as a constant integer expression. The operand can be a data type, variable, or array. Here's an expanded example to illustrate its usage:

#include <stdio.h>

int main() {
int num = 10;
float pi = 3.14;
char ch = 'A';
char arr[5] = {'a', 'p', 'p', 'l', 'e'};

printf("Size of int: %zu bytes\n", sizeof(num));
printf("Size of float: %zu bytes\n", sizeof(pi));
printf("Size of char: %zu bytes\n", sizeof(ch));
printf("Size of char array (arr): %zu bytes\n", sizeof(arr));
printf("Size of each element in the char array (arr): %zu bytes\n", sizeof(arr[0]));

return 0;
}

In this example, we have defined four variables of different data types (int, float, char, and an array of chars) and used the sizeof operator to print their respective sizes. We also demonstrate how to use sizeof with arrays to determine both the size of the entire array and each individual element. The output will be something like:

Size of int: 4 bytes
Size of float: 4 bytes
Size of char: 1 bytes
Size of char array (arr): 5 bytes
Size of each element in the char array (arr): 1 bytes

Note that the size of an integer on your system may vary depending on the architecture and compiler used.

Internals (Optional)

Understanding how the sizeof operator works internally can be helpful for advanced C programmers. At a high level, the sizeof operator calculates the size of its operand by looking up the size of the data type in the symbol table maintained by the compiler. For variables and arrays, the size is determined based on their declared type and dimensions.

Worked Example

Let's walk through an example where we use the sizeof operator to create a dynamic array and calculate its memory usage:

#include <stdio.h>
#include <stdlib.h>

int main() {
int n, i;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);

int* arr = (int*) malloc(n * sizeof(int));

for (i = 0; i < n; i++) {
arr[i] = i * 2;
}

printf("Size of the dynamic array: %zu bytes\n", sizeof(arr));
printf("Size of each element in the dynamic array: %zu bytes\n", sizeof(arr[0]));
printf("Total memory allocated for the dynamic array: %zu bytes\n", sizeof(arr) * n);
free(arr);

return 0;
}

In this example, we first ask the user to input the number of elements in the array. We then dynamically allocate memory for the array using malloc(), ensuring that it's large enough to hold n integers. After populating the array with values, we use the sizeof operator to calculate both the size of each individual element and the total memory allocated for the dynamic array. Finally, we free the allocated memory using free().

Common Mistakes

  1. Using sizeof on a variable without parentheses: In C, when you omit parentheses around an operand in the sizeof operator, it will be treated as a type rather than a variable. This can lead to unexpected results. Always use parentheses for clarity and correctness:

Wrong: int x = 10; printf("%zu", sizeof x);

Correct: int x = 10; printf("%zu", sizeof(x));

  1. Assuming the size of an array is equal to the number of elements: This is a common misconception among beginners. The size of an array in C is always the product of its number of elements and the size of each element, not just the number of elements. For example, an array of 10 integers would have a size of 40 bytes (assuming an integer occupies 4 bytes).
  1. Not accounting for memory overhead when calculating array sizes: When working with dynamic arrays, don't forget to account for the memory overhead of pointers and other data structures used to manage the array. This can help you avoid running out of memory during runtime.
  1. Using sizeof on a non-aggregate type or incomplete type: The sizeof operator cannot be applied to non-aggregate types (e.g., function pointers, structs with no named members) or incomplete types (structs or unions declared but not fully defined).

Practice Questions

  1. Write a program that uses the sizeof operator to determine the size of a struct containing three integers, two floats, and a character array with a fixed length of 20 characters.
  2. A dynamic array is created using malloc() to store 100 integers. What should be the size of the memory allocated for this array in bytes? (Assuming an integer occupies 4 bytes.)
  3. Given the following code snippet, what output will be printed by the program?
int main() {
char ch = 'A';
printf("%zu\n", sizeof(ch) + 1);
}

FAQ

Q: Can I use sizeof to calculate the size of a function or a struct?

A: Yes, you can use the sizeof operator to determine the size of a function or a struct in C. However, keep in mind that the size may vary based on factors like compiler optimizations and padding.

Q: Is it possible to calculate the size of an array at runtime using sizeof?

A: No, you cannot use the sizeof operator to determine the size of an array at runtime because its size is a compile-time constant. Instead, you can use other methods like sizeof arr / sizeof arr[0] to calculate the number of elements in an array dynamically.

Q: Why do I sometimes see people using sizeof(type) instead of just type when declaring variables?

A: Using sizeof(type) explicitly specifies the size of the variable at compile time, which can help avoid potential issues with compiler optimizations or memory alignment. However, in most cases, simply declaring a variable without sizeof is sufficient and more readable.

Q: Why does the size of an empty array differ from the size of its element type?

A: An empty array has no elements but still occupies some memory to store its size information. The exact amount of memory depends on the compiler and platform, but it is typically smaller than the size of a single element.