sizeof() operator is a very useful tool that helps programmers understand how much memory a variable or data type occupies in the computer's memory.
- It is a compile-time unary operator, which means the compiler calculates the size before the program runs.
- Used to determine the size of a variable or data type in bytes.
- The result is of type size_t, which is an unsigned integer type used to represent sizes.
C++
#include <stdio.h>
int main()
{
int i;
float f;
char c;
printf("Size of int: %d bytes\n", (int)sizeof(i));
printf("Size of float: %d bytes\n", (int)sizeof(f));
printf("Size of char: %d bytes\n", (int)sizeof(c));
return 0;
}
OutputSize of int: 4 bytes
Size of float: 4 bytes
Size of char: 1 bytes
Usage of sizeof() operator
sizeof() operator is used in different ways according to the operand type.
1. When the operand is a Data Type: When sizeof() is used with the data types such as int, float, char... etc it simply returns the amount of memory allocated to that data types.
C
#include <stdio.h>
int main()
{
printf("Size of char: %lu bytes\n", sizeof(char));
printf("Size of int: %lu bytes\n", sizeof(int));
printf("Size of float: %lu bytes\n", sizeof(float));
printf("Size of double: %lu bytes\n", sizeof(double));
printf("Size of pointer: %lu bytes\n", sizeof(void *));
return 0;
}
OutputSize of char: 1 bytes
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of pointer: 8 bytes
Note: sizeof() may give different output according to machine, we have run our programs on a 64-bit gcc compiler.
2. When the operand is an expression: When sizeof() is used with the expression, it returns the size of the expression.
C
#include <stdio.h>
int main()
{
int a = 0;
double d = 10.21;
printf("%lu", sizeof(a + d));
return 0;
}
As we know from the first case size of int and double is 4 and 8 respectively, a is int variable while d is a double variable. The final result will be double, Hence the output of our program is 8 bytes.
Compiler Time Operator
sizeof() is a compile-time operator. compile time refers to the time at which the source code is converted to a binary code. It doesn't execute (run) the code inside ().
C
#include <stdio.h>
int main(void)
{
int y;
int x = 11;
// value of x doesn't change
y = sizeof(x++);
// prints 4 and 11
printf("%i %i", y, x);
return (0);
}
If we try to increment the value of x, it remains the same. This is because x is incremented inside the parentheses and sizeof() is a compile-time operator.
Need of Sizeof
1. To find out the number of elements in an array: Sizeof can be used to calculate the number of elements of the array automatically.
C
#include <stdio.h>
int main()
{
int arr[] = {1, 2, 3, 4, 7, 98, 0, 12, 35, 99, 14};
printf("Number of elements:%lu ", sizeof(arr) / sizeof(arr[0]));
return 0;
}
OutputNumber of elements:11
2. To allocate a block of memory dynamically: sizeof is greatly used in dynamic memory allocation. For example, if we want to allocate memory that is sufficient to hold 10 integers and we don't know the sizeof(int) in that particular machine. We can allocate with the help of sizeof.
C++
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *arr;
// Allocate memory for 10 integers
arr = (int *)malloc(10 * sizeof(int));
if (arr == NULL)
{
printf("Memory allocation failed!\n");
return 1;
}
// Initialize and print the array
for (int i = 0; i < 10; i++)
{
arr[i] = i + 1;
printf("%d ", arr[i]);
}
// Free the allocated memory
free(arr);
return 0;
}
Output1 2 3 4 5 6 7 8 9 10
For more information, refer to the article - Allocate a Block of Memory Dynamically.
Explore
C Basics
Arrays & Strings
Pointers and Structures
Memory Management
File & Error Handling
Advanced Concepts