6.8 – sizeof operator in C language

by subbu on September 29, 2013

Sizeof operator in C language:

We have been using the sizeof operator to demonstrate how much size does a data type takes in different environments and to demonstrate the default type of constants in C language. Now in this session, we will see the complete profile of sizeof operator.

sizeof is a unary operator

Only one operand is used with the sizeof operator so called unary operator. Here the operand may be a data type, variable, constant or an expression. It returns the size that would be taken by the operand in bytes.

Sizeof to find the size of data types

/* finding the size of data types */
#include<stdio.h>
int main()
{
 printf("Size of short %d Bytes",sizeof(short));
 printf("\nSize of long %d Bytes",sizeof(long));
 printf("\nSize of char %d Bytes",sizeof(char));
 return 0;
}

Output:
Size of short 2 Bytes
Size of long 4 Bytes
Size of char 1 Bytes

Here in this example, sizeof operator returns how much space a particular type will take in the memory in terms of Bytes.
sizeof(short) gives 2 because short type takes 2 bytes, sizeof(long) gives 4 because long type takes 4 bytes and sizeof(char) gives 1 because char type takes 1 Byte in C language.

sizeof to find the size of variables

#include<stdio.h>
int main()
{
 short a[5];
 short b[]={12,34,56,77,98,44};
 unsigned long x;
 printf("Size of array a is %d Bytes",sizeof(a));
 printf("\nSize of array b is %d Bytes",sizeof(b));
 printf("\nSize of variable x is %d Bytes",sizeof(x));
 return 0;
}

Output:
Size of array a is 10 Bytes
Size of array b is 12 Bytes
Size of variable x is 4 Bytes

Here, the sizeof operator gives the size of arrays and a variable.
sizeof(a) gives 10 because a has 5 short elements of two bytes each, sizeof(b) gives 12 because b has 6 short elements of two bytes each and sizeof(x) gives 4 because long type takes 4 bytes.

sizeof to find the size of constants

#include<stdio.h>
int main()
{
 printf("Size of 12 is %d Bytes",sizeof(12));
 printf("\nSize of 'a' is %d Bytes",sizeof('a'));
 printf("\nSize of 900L is %d Bytes",sizeof(900L));
 printf("\nSize of 12.45 is %d Bytes",sizeof(12.45));
 printf("\nSize of 12.45f is %d Bytes",sizeof(12.45f));
 return 0;
}

Output in Turbo C:
Size of 12 is 2 Bytes
Size of ‘a’ is 2 Bytes
Size of 900L is 4 Bytes
Size of 12.45 is 8 Bytes
Size of 12.45f is 4 Bytes

Output in MinGW gcc:
Size of 12 is 4 Bytes
Size of ‘a’ is 4 Bytes
Size of 900L is 4 Bytes
Size of 12.45 is 8 Bytes
Size of 12.45f is 4 Bytes

The default type of any integer constant in C language is “int”. Hence the sizeof(12) gives 2 in Turbo C and 4 in MinGW because int takes 2 bytes in Turbo C and 4 bytes in MinGW.
The default type of any real constant in C language is “double”. Hence the sizeof(12.45) gives 8.
sizeof(900L) gives 4 because suffix “L” makes the integer constant as long type otherwise it would have been int type.
sizeof(12.45f) gives 4 because suffix “f” makes the real constant as float type otherwise it would have been of double type.
sizeof(‘a’) gives the size of 97 (int) rather the size of character ‘a’ where 97 is the ASCII value of ‘a’

sizeof with expressions:

#include<stdio.h>
int main()
{
 int x,y;
 printf("Size of 10+14.50 is %d Bytes",sizeof(10+14.50));
 printf("\nSize of 10+14.50f is %d Bytes",sizeof(10+14.50f));
 printf("\nSize of 10+40L is %d Bytes",sizeof(10+40L));
 printf("\nSize of x>y is %d Bytes",sizeof(x>y));
 return 0;
}

Output in Turbo C:
Size of 10+14.50 is 8 Bytes
Size of 10+14.50f is 4 Bytes
Size of 10+40L is 4 Bytes
Size of x>y is 2 Bytes

Output in MinGW:
Size of 10+14.50 is 8 Bytes
Size of 10+14.50f is 4 Bytes
Size of 10+40L is 4 Bytes
Size of x>y is 4 Bytes

We know that in mixed mode arithmetic expression, the type of result would be promoted to higher type.
sizeof(10+14.50) gives 8 because 14.50 is treaded as double type, sizeof(10+14.50f) gives 4 because 14.50f is treated as float type, sizeof(10+40L) gives 4 because 40L is considered as long type and sizeof(x>y) gives 2 in Turbo C and 4 in MinGW because x<y is a relational expression, gives either 1 or 0 which is again of int type.

Calculating the length of array using sizeof operator:

There is no function to find the length of an array in C language. We can find the length of an array using the sizeof operator. Total size of array division by the size of any element gives the length of array

#include<stdio.h>
int main()
{
 int len;
 float x[]={12.45f,45.78f,44.56f,98.23f,11.3456f};
 printf("Size of total array %d",sizeof(x));
 printf("\nSize of each element %d",sizeof(x[0]));
 len=sizeof(x)/sizeof(x[0]);
 printf("\nLength of array %d",len);
 return 0;
}

Output:
Size of total array 20
Size of each element 4
Length of array 5

sizeof is a compile time operator

Most of the unary and binary operators execute during the run time that is during the execution of program. Where as sizeof operator evaluates and places its result in the object code.
Hence when we write any expression in sizeof, the expression would not be evaluated but, only its size would be given.

#include<stdio.h>
int main()
{
 short x=10,s;
 s=sizeof(x=x+20);              /* instruction to compiler */
 printf("size of x is %d",s);
 printf("\nValue of x is %d",x);
 return 0;
}

Output:
Size of x is 2
Value of x is 10

Though we are incrementing the value of x by 20 in sizeof operator, it is observed that the value of x is not changed because the compiler substitutes sizeof(x=x+20) by its result 2 like

s=2;

Previous post:

Next post: