9

I saw some code which used sizeof directly and wondered if it is standard C. To my surprise, it was working just fine. Here is an example:

#include <stdio.h>
#include <string.h>

int main()
{
   char buff[255];
   printf("size %d\n", sizeof buff);
   return 0;
}

Output: size 255

As you can see, in the above example I used sizeof <variable> instead of sizeof(<variable>)

Please throw more light on it.

Sandeep
  • 18,356
  • 16
  • 68
  • 108

1 Answers1

21

"When sizeof's operand is a type, it has to be enclosed in parentheses. But when sizeof's operand is a variable, this is not required."--Expert C Programming: Deep C secrets. And from c11 standard:

sizeof unary-expression
sizeof ( type-name )
Yulong Ao
  • 1,199
  • 1
  • 14
  • 22