0

Problem Statement:-

What would be the output for the following program?

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

I am working with 32 bit gcc compiler. And it is printing the output as-

4, 2, 4

I am confused why it is printing 4 for sizeof ( '3' ). 3 is in single inverted commas so it will be a character right? Then it shouldn't print out 4? Am I right?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

1 Answers1

4

Character literals are integers in C. That means sizeof('3') is the same as sizeof(int). On your machine, that looks like 4.

Editorial notes:

  1. Don't use %d as the format string when printing sizeof results. sizeof returns a size_t, so you should use %zu instead.

  2. You should use a correct prototype for main. That is: int main(void) or int main(int argc, char **argv). Not just main().

  3. If you use the correct prototype for main, make sure your program returns something. return 0; at the end, for example.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469