tags:

views:

355

answers:

4

sample code:

main ()
{
printf ("size = %d\n", sizeof (main));
}
A: 

The size of the function pointer, which will be the same as the size of any other pointer on your host architecture. Note that, when you are referring to main() as main, you are referring to the address of main().

Amit
This is not correct - see other answers.
Paul R
+3  A: 

ISO C++ forbids applying sizeof to an expression of function type.

ISO/IEC 14882 on C++ says (section 5.3.3):

"The size operator shall not be applied to an expression that has function or incomplete type,..."

The same hold for standard C (ISO/IEC 9899:1999) section 6.5.3.4:

"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."

zoli2k
What is the rational behind it?
Yassin
This question is tagged as C, so the C++ standard has no bearing.
Matthew T. Staebler
@Aeth many parts of the C++ standard are adopted from C. See the updated answer with added reference.
zoli2k
+8  A: 

C standard forbids it - when compiled with gcc -pedantic, it produces invalid application of ‘sizeof’ to a function type warning.

However gcc compiles it and returns 1 for sizeof(main), and it is not a size of function pointer.

It seems to be compiler-dependent.

qrdl
Matthew T. Staebler
@Aeth You are completely correct, I got the same result with latest and greatest GCC 4.5. Post edited.
qrdl
A: 

dont use sizeof with fuction

sizeof +int,char,double,,,,

or

sizeof + number

gcc