Syntax of sizeof() in C
The syntax of the sizeof() operator is simple and straightforward. It can be used in two ways:
sizeof(type);
or
sizeof(expression);
-
type: Represents any valid C data type (e.g., int, float, char, etc.).
- expression: Refers to a variable or expression whose size needs to be determined.
Return Type of sizeof() Operator in C
The sizeof() operator returns the size of the data type or expression in bytes. The return type of sizeof() is size_t, which is an unsigned integer type defined in the <stddef.h> header file. Since it returns an unsigned value, the result will always be positive.
int x = 10;
printf("%zu", sizeof(x)); // Output will be the size of an int, typically 4 bytes.
This helps developers ensure accurate memory management, especially when dealing with dynamic memory allocation.
Read about Bitwise Operators in C And Floyd's Triangle in C here.
Example of sizeof() in C
Here is a C program that shows how to use the sizeof operator in the C language:
#include <stdio.h>
int main()
{
// Type
char a;
int b;
float c;
double d;
printf("Size of char = %lu\n", sizeof(a));
printf("Size of int = %lu\n", sizeof(b));
printf("Size of float = %lu\n", sizeof(c));
printf("Size of double = %lu", sizeof(d));
//Expression
int i = 5;
int expression_size = sizeof(i++);
// Displaying the size of the operand
printf("\nSize of Expression = %d", expression_size);
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output
Size of char = 1
Size of int = 4
Size of float = 4
Size of double = 8
Size of Expression = 4
For practice, you can implement this code on online compiler.
Must Read Passing Arrays to Function in C
How Does the sizeof() Operator Work in C?
The sizeof() operator in C works at compile time to calculate the memory required by a data type or expression in bytes. When used, it evaluates the data type or variable and returns the size in bytes, considering the system architecture. The size varies for different data types and systems (e.g., 4 bytes for an integer on a 32-bit system). Since it operates during compilation, it ensures efficient memory management by providing accurate size information without needing program execution.
Application of sizeof() Operator in C
Memory Allocation for Arrays
The sizeof() operator is used to determine the total memory occupied by an array. It calculates the size of the entire array by multiplying the number of elements by the size of each element. This is helpful in cases where you need to pass the array to functions and manage memory efficiently.
int arr[10];
printf("Size of array: %zu\n", sizeof(arr));

You can also try this code with Online C Compiler
Run Code
Output
Size of array: 40
Explanation: The sizeof() operator is used to find the total memory allocated for the array, which is 10 elements × 4 bytes each.
Dynamic Memory Allocation
When allocating memory dynamically using functions like malloc(), the sizeof() operator ensures that the correct amount of memory is allocated based on the data type. This prevents memory overflows and under-allocations, making dynamic memory management safer.
int* ptr = (int*)malloc(5 * sizeof(int));
printf("Size of dynamically allocated memory: %zu\n", 5 * sizeof(int));

You can also try this code with Online C Compiler
Run Code
Output
Size of dynamically allocated memory: 20
Explanation: It helps allocate memory dynamically, calculating memory based on the size of data types.
Need for the sizeof() Operator in C
Handling Platform Variability
The sizeof() operator automatically adjusts to the platform's architecture, ensuring that the correct memory size is calculated for data types, regardless of whether the system is 32-bit or 64-bit. This allows the same C code to be portable across different platforms.
printf("Size of int: %zu bytes\n", sizeof(int));

You can also try this code with Online C Compiler
Run Code
Output
Size of int: 4 bytes (on a 32-bit system)
Efficient Memory Management
When working with structures or complex data types, sizeof() ensures proper memory allocation, considering factors like padding and alignment. This helps to optimize memory usage, prevent wastage, and avoid segmentation faults during program execution.
struct Example {
char a;
int b;
};
printf("Size of struct: %zu bytes\n", sizeof(struct Example));

You can also try this code with Online C Compiler
Run Code
Output
Size of struct: 8 bytes
Advantages of sizeof() Operator in C
-
Compile-Time Evaluation: sizeof() is evaluated at compile time, ensuring no performance overhead during execution.
-
Platform Independence: Automatically adjusts sizes for different system architectures, making code portable.
-
Efficient Memory Use: Helps prevent memory overflow and underflow by determining precise memory requirements.
- Dynamic Memory Allocation: Critical for allocating exact memory space when using dynamic memory functions like malloc().
Frequently Asked Questions
Can we apply the sizeof operator to every expression in C?
The sizeof operator cannot be used on expressions with function or incomplete types or on expressions that indicate a bit-field member.
Can the sizeof() operator be used with any data type?
Yes, the sizeof() operator can be used with any data type in C, including primitive types (e.g., int, float, char), derived types (e.g., arrays, pointers), and user-defined types (e.g., structures, unions). It returns the size in bytes.
Can we use the sizeof operator to get the number of elements in an array?
Yes we can get the size of array by using sizeof operator.
size= sizeof(array)/ sizeof(array[0])
Conclusion
In this article, we explored the sizeof() operator in C. We began by examining its syntax and how it works, followed by its applications in memory allocation and efficient data management. We also discussed the importance of the sizeof() operator in handling platform variability and ensuring optimal memory usage.