Sizeof () operator in C

Consider the program

main()  
{  
 printf("%d %d %d",sizeof('3'),sizeof("3"),sizeof(3));  
}

output from gcc compiler :

4 2 4

Why is this so?

+5
source share
4 answers

Assuming you are working on a 32-bit system:

the literal '3' character sizeof is 4, since character literals are int in C (but not in C ++).

sizeof "3" is 2 because it is an array literal with a length of 2 (number 3 plus a NULL terminator).

sizeof literal 3 is 4 because it is an int.

+13
source

A few considerations to keep in mind:

  • sizeofnot a function, this is an operator. It returns the type size in units sizeof char. In other words, sizeof charalways 1.
  • '3' - int
  • "3" - char[2], 3, .
  • 3 int

:

  • a int 4 char
  • char[2] 2 char s
+12

K R,

, , ints 16 , , , 32 , short - , int, .

+3

sizeof() ,

0

All Articles