The sizeof operator in C programming is used to determine the amount of space or memory any object/operand is taking.It is a kind of unary operator which works at compile time. It returns the memory occupied in terms of bytes .
Sizeof Operator
While using the sizeof operator in C programming, always “%lu” or “%zu” format specifier is used. We can use the sizeof operator with normal format specifiers also and no error will be generated but the correct way to use sizeof operator is using %lu or %zu format speciffier.
This sizeof operator can be used to calculate the size of a variable as well as of a data type also.
Syntax
sizeof(variable);
The above mentioned syntax is used to calculate the size of a variable.
Sizeof(data_type);
The above mentioned syntax is used to calculate the size of a datatype.
#include<stdio.h>
int main() {
int a = 16;
printf("Size of variable a : %lu\n",sizeof(a));
printf("Size of int data type : %lu\n",sizeof(int));
printf("Size of char data type : %lu\n",sizeof(char));
printf("Size of float data type : %lu\n",sizeof(float));
printf("Size of double data type : %lu\n",sizeof(double));
return 0;
}
Output:
Size of variable a : 4
Size of int data type : 4
Size of char data type : 1
Size of float data type : 4
Size of double data type : 8
NOTE:The sizeof operator simply returns the amount of memory allocated to that particular variable or object.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment