GET EDUCATE

Contents
Basics of C Programming

⯈Basic C Programs

sizeof operator in C

  • It is a Unary operator.
  • sizeof is a keyword.
  • This operator gives the size in number of bytes taken by a constant, variable or an expression.
  • sizeof gives the answer in long long int form.

You may watch the video posted at the end of this page.

Examples

long long int y = sizeof(int);

y = 4 (Depends on compiler, some compiler takes 2 bytes)


 

long long int y = sizeof(char);

y = 1


 

long long int y = sizeof(double);

y = 8


 

long long int y = sizeof(float);

y = 4


 

long long int y = sizeof(10);

y = 4 

Note: By default, all integers are of int datatype. To make it of long type we add after the number.


long long int y = sizeof(10L);

y = 8 (10L is of type long)


long long int y = sizeof(‘A’);

y = 1


long long int y = sizeof(1.23);

y = 8

Note: By default, all real numbers are of double datatype. To make real numbers of float datatype we add ‘f’ after the number.


long long int y = sizeof(1.23f);

y = 4 , 1.23f is of type float.


 

Note: The expression inside the sizeof operator doesn't gets evaluated.

Example 1:

int a = 5;

int b = sizeof(++a);

The expression inside sizeof, ++a, won’t be evaluated.

Therefore,

a = 5 and b = 4.


 

Example 2:

int x = sizeof(4+4.0);

x = 8

The highest datatype will be considered, which is double here.

Scroll to Top