← Back to C Course | Chapter 1: Introduction & Basics | Lesson 13 of 21

C sizeof Operator

The sizeof operator returns the number of bytes a type, variable, array, struct, or expression occupies, evaluated at compile time, essential for writing portable, size-aware C code.
Syntax
c
sizeof(data_type);
sizeof(variable_name);

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

The sizeof Operator

The sizeof operator returns the number of bytes occupied by a type or a variable, evaluated at compile time, and is essential for writing portable code that doesn't assume a fixed size for any type.

Example: The sizeof Operator

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare x and set it to 5
	int x = 5;
	// Print formatted text to the screen
	printf("%zu", sizeof(x));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Size of Basic Types

Applying sizeof to basic types like char, int, and double reveals how much memory each one uses on the current system, which can vary between different compilers and platforms.

Example: Size of Basic Types

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("char: %zu, int: %zu, double: %zu", sizeof(char), sizeof(int), sizeof(double));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Size of Arrays

sizeof applied to an array returns the total number of bytes used by every element combined, and dividing that by the size of a single element is the standard technique for computing an array's length in C.

Example: Size of Arrays

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int numbers[5] = {1, 2, 3, 4, 5};
	int length = sizeof(numbers) / sizeof(numbers[0]);
	// Print formatted text to the screen
	printf("%d", length);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Size of Structures

sizeof applied to a struct returns the total memory the structure occupies, which can be larger than the sum of its individual members due to padding the compiler adds for alignment.

Example: Size of Structures

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named Point
struct Point {
	char label;
	int x;
};
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%zu", sizeof(struct Point));
	// Return 0 to signal that the program finished successfully
	return 0;
}

sizeof with Expressions

sizeof can be applied directly to an expression rather than just a type or variable, returning the size of whatever type that expression would evaluate to, without actually computing the expression's value at runtime.

Example: sizeof with Expressions

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare a and set it to 5
	int a = 5;
	// Print formatted text to the screen
	printf("%zu", sizeof(a + 1.0));
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using sizeof on an array parameter inside a function, where it gives the size of a pointer instead of the array.
  2. Printing the result of sizeof with %d instead of %zu, which can be wrong on 64-bit systems.
  3. Assuming sizeof(struct) is the sum of its members, when padding can make it larger.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.