C sizeof() function

  • By using sizeof() function, we can find the size of given argument.
  • sizeof function always returns unsigned int value which value is greater than zero(0).

Example:

voind main()
{
int i;
float f;
char ch;
printf(“\n size of int: %d”,sizeof(i));
printf(“\n size of float: %d”,sizeof(f));
printf(“\n size of char: %d”,sizeof(ch));
}

output:

size of int:2
size of float:4
size of char:1

Few More Examples:

sizeof(25) — 2
sizeof(25u) — 2
sizeof(25l) — 4
sizeof((25lu) — 4
sizeof(short) — 2
sizeof(long) — 4
sizeof(signed) — 2
sizeof(unsigned) — 2
sizeof(char) –1
sizeof((‘A’) –2(because sizeof(65) -2)

Note: in sizeof function all expression can be evaluated except assignment operator.

Example:

void main()
{
int a;
a=10;
printf(“\n size of a:%d”,sizeof(++a));
printf(“\na=%d”,a);
}

Powered by k2schools