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
  • 154,301
  • 39
  • 440
  • 740

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,783
  • 6
  • 53
  • 70
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
0

sizeof(char) equals 1, always and everywhere.

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

alk
  • 69,737
  • 10
  • 105
  • 255