ProAnswers.org

Sizeof operator in C language

Can you tell me about sizeof operator in C language?

1 Like

sizeof() operator:-

The sizeof() operator is used to return the size of the operands in bytes.

Example:-

#include
#include
void main()
{
int a;
clrscr();
printf("%d
“,sizeof(a));
printf(”%d/n",sizeof(int));
getch();
}

OUTPUT = 2
OUTPUT = 2

Imp point about sizeof() Operator:-

1)sizeof() is an operator which is also a keyword but it looks like a function.
2)sizeof() operator is only applicable to variable and constants but can not be applied to function.

Example:

printf("%d",sizeof(clrscr));

this will give an error: sizeof may not be applied to functions.

Reason: here clrscr() is a function. sizeof operator cannot be applied to functions. It can only be used with varibles and constants.

3)sizeof() operator does not eveluate arithmatic expresion

Example:

voimain()
{
int h = 5;
printf("%d",sizeof(h++)); // sizeof operator does not evaluate this step
printf("%d",h);
}
OutPut = 5

4)when size of a data type is measured,() is must.

void main()
{
int a;
clrscr();
printf("%d/n",sizeof(int)); // () is must here with sizeof
getch();
}

OUTPUT = 2

  1. () is not must when varible and constants are used.

void main()
{
int a;
clrscr();
printf("%d
",sizeof a); // () is not must here with sizeof
getch();
}

OUTPUT = 2

Thank you!!!

always welcome