sizeof() Operator in C: Usage and Examples
Table of Content:
The sizeof is a unary operator which returns the size of data (constant, variables, array, structure etc).
sizeof operator on Datatype
When an 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 is allocated to that data types.
Program
#include
int main()
{
printf("%d\n",sizeof(char));
printf("%d\n",sizeof(int));
printf("%d\n",sizeof(float));
printf("%d", sizeof(double));
return 0;
}
Output
1
4
4
8Press any key to continue . . .
1 4 4 8Press any key to continue . . .
sizeof operator on Variable
Program
#include int main() { int a, e[10]; float b; double c; char d; printf("Size of int=%lu bytes\n",sizeof(a)); printf("Size of float=%lu bytes\n",sizeof(b)); printf("Size of double=%lu bytes\n",sizeof(c)); printf("Size of char=%lu byte\n",sizeof(d)); printf("Size of integer type array having 10 elements = %lu bytes\n", sizeof(e)); return 0; }
Output
Size of int=4 bytes
Size of float=4 bytes
Size of double=8 bytes
Size of char=1 byte
Size of integer type array having 10 elements = 40 bytes
Press any key to continue . . .
sizeof operator on operand is an expression
When operand is an expression. When sizeof() is used with the expression, it returns size of the expression. Let see example:
Program
#include int main() { int a = 0; double d = 10.21; printf("%d \n", sizeof(a+d)); return 0; }
Output
8
Press any key to continue . . .
As we know from first case size of int and double is 4 and 8 respectively, a is int variable while d is a double variable. final result will be a double, Hence output of our program is 8 bytes.
Need of Sizeof
To find out number of elements in a array. Sizeof can be used to calculate number of elements of the array automatically. Let see Example :
Program
#include int main() { int arr[] = {1, 2, 3, 4, 7, 98, 0, 12, 35, 99, 14}; printf("Number of elements :%d \n", sizeof(arr)/sizeof(arr[0])); return 0; }
Output
Number of elements :11
Press any key to continue . . .
To allocate block of memory dynamically.
sizeof is greatly used in dynamic memory allocation. For example, if we want to allocate memory for which 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.
Program
int *ptr = malloc(10*sizeof(int));
Important Program
Program 1:
#include int main() { printf( "\n %d %d \n", sizeof('3'), sizeof("3"), sizeof(3) ) ; }
Output
4 2
Press any key to continue . . .
Program 2:
In this case x and y will not hold the same value
x = sizeof(pi); this will return 4 in case of 32 bit machine.
y = sizeof(3.14); this will return double precion value at is 8 in case of same 32 bit machine.
#include void main() { int x,y; float pi=3.14; x = sizeof(pi); y = sizeof(3.14); if(x==y) { printf("Size is same \n"); } else{ printf("Size is not same \n"); } }
Output
Size is not same
Press any key to continue . . .
#include
void main()
{
int x,y;
float pi=3.14;
x = sizeof(pi);
y = sizeof(3.14);
if(x==y)
{
printf("Size is same \n");
}
else{
printf("Size is not same \n");
}
printf("%d\n",x);
printf("%d\n",y);
}
Output
Size is not same
4
8
Press any key to continue . . .
Size is not same 4 8 Press any key to continue . . .