C sizeof Operator
In this page:
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- Using
sizeofon an array parameter inside a function, where it gives the size of a pointer instead of the array. - Printing the result of
sizeofwith%dinstead of%zu, which can be wrong on 64-bit systems. - Assuming
sizeof(struct)is the sum of its members, when padding can make it larger.
Chapter Quiz — Complete all 21 topics to unlock
0/21 topics done
Complete these topics first:
- C Introduction
- C History & Features
- C Environment Setup
- C First Program
- C Syntax & Structure
- C Statements
- C Comments
- C Keywords & Identifiers
- C Data Types
- C Character Data Type
- C Numeric Data Types
- C Decimal (Floating-Point) Numbers
- C sizeof Operator
- C Extended Data Types
- C Type Conversion
- C Booleans
- C Variables
- C Changing Variable Values
- C Multiple Variables
- C Constants
- C Fixed-Width Integers