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 . . .
- Question 1: What is the use of sizeof() function in C?
Related Questions
- Assignment 1: Storage size of int datatype
- Assignment 2: Storage size of char datatype
- Assignment 3: Storage size of unsigned char datatype
- Assignment 4: Storage size of signed char datatype
- Assignment 5: Storage size of unsigned int datatype
- Assignment 6: Storage size of unsigned short datatype
- Assignment 7: Storage size of long datatype
- Assignment 8: Storage size of unsigned long datatype
- Assignment 9: C program long double datatype
- Assignment 10: Storage size of float datatype
- Assignment 11: Storage size of double datatype
- Assignment 12: Storage size of long double datatype
- Assignment 13: Storage size and range of floating point datatype
- Assignment 14: sizeof() operator, Returns the size of a variable.
- Assignment 15: sizeof() operator, Returns the size of a variable.
- Assignment 16: sizeof() operator, Returns the size of a variable.
- Assignment 17: Write a program to calculate the size in bytes required by data items and in-built data types of C using the "sizeof" operator.
- Assignment 18: Find the size of various types of datatypes in c programming language