2

Why is the result of:

sizeof(function_name)?

1? Somebody in community said that sizeof(main) is 1 but sizeof(main()) is 4. Here main is returning int, so it is returning 4. but if we do declaration as char main() instead of int main() it will return 1 only.

Shafik Yaghmour
  • 148,593
  • 36
  • 425
  • 712

2 Answers2

3

It's illegal to use sizeof on a function.

C99 §6.5.3.4 The sizeof operator

The sizeof operator shall not be applied to an expression that has function type or an incomplete type, to the parenthesized name of such a type, or to an expression that designates a bit-field member.

So if foo is the function name, sizeof(foo) is illegal, but sizeof(foo()) applies sizeof on the return value of foo(). main() returns int in standard C, it's 4 bytes on most machines today.

Filipe Gonçalves
  • 20,077
  • 6
  • 49
  • 66
Yu Hao
  • 115,525
  • 42
  • 225
  • 281
0

sizeof(char) equals 1, always and everywhere.

So everything declared char would do also, and this already at compile-time.

alk
  • 68,300
  • 10
  • 92
  • 234