Table of Contents
sizeof in C
sizeof in C is used to return the size of a variable. Different variables of different types have different sizes. In this article we will explore this operator and see its use case.
Example
Let us find the sizes of different types of variables.
#include<stdio.h> int main() { int a; char b; float c; double d; printf("size of integer variable a is : %d\n",sizeof(a)); printf("size of character variable b is : %d\n",sizeof(b)); printf("size of float variable c is : %d\n",sizeof(c)); printf("size of double variable d is : %d\n",sizeof(d)); return 0; }
Output
Thus, we learned about the size of int, char, float, double data types from the above code.
Note: Whenever we declare an integer variable, a space in the memory equal to 4 bytes gets occupied by it. It doesn’t matter whether we assign a value to the variable or not. Space gets allocated by declaration.
The size of the variables differs for 32-bit and 64-bit compilers.
We can also directly pass the data type in the size of() operator to know its size.
printf(“%d”, sizeof(int)); statement is valid.
0 Comments