Sizeof Operator MCQ : sizeof operator and Function



Must Read Tip To Solve Following Problem :

  1. First one must understand that - sizeof is not an function it’s an operator.
  2. Inside C Program we can calculate size of all data types including user defined and in built data types.
  3. However Sizeof Operator cannot calculate Size when we pass function name as parameter.
  4. However Sizeof Operator can take address of Function because in c “Address of variable or function” is always of type integer.

Sizeof Operator Question :

Now try to solve following question by referring above tips -

#include<stdio.h>
int main()
{
printf("\n%d",sizeof(&main)+2);
return(0);
}

Output with Explanation

4

Re-commanded Reading : Size of structure without Sizeof Operator

Explanation :

  1. &main will gives us Address where we have defined the actual function.
  2. sizeof operator will treat address as “integer” data type.
  3. In the 32 bit compiler, Size of Integer = 2 bytes (in Turbo C/C++ Compiler), therefor output is 4.
  4. Requirement of the memory for storing the integer data type will vary from from compiler to compiler.
  5. Consider the below program -
    sizeof operator

  6. Now we have main function at any random memory location say 1000 then -
    &main; = 1000
    
  7. Thus 1000 is an address of the memory location thus we can say that sizeof function will always return us the size of starting address.
  8. We cannot compute the sizeof the function by passing the function name of address of function as parameter to sizeof operator.

Must Read : Sizeof Pointer | Nested Sizeof | Size of void pointer