top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

C: why the sizeof main() is 2 byte?

0 votes
238 views
C: why the sizeof main() is 2 byte?
posted Jul 17, 2014 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

sizeof is a compile-time constant and main may not been called by that time.

However manul of C clearly says "The size operator shall not be applied to an expression that has function or incomplete type" however sizeof() can take any valid identifier, but it won't return a valid (an honestly true and valid) result for function names.

So there is no explanation why sizeof(main) is returning 2, however i wrote a small program as following which is giving me 1.

main()
{
  printf("%d\n", sizeof(main));
}
answer Jul 18, 2014 by Salil Agrawal
Similar Questions
0 votes

Used the sizeof of function, which gives 1; why?
I want to know the size of the entire function. How to achive it?

#include <stdio.h>
void (*p)(int); 
void test_func(int data)
{
  printf("%d\n",data);
}

main(void)
{
    p = test_func;
    (*p)(4);
    printf("%d",sizeof(test_func));
}
0 votes

A related query, can someone help?

...