Sizeof Operator MCQ : sizeof operator and Function
Must Read Tip To Solve Following Problem :
- First one must understand that - sizeof is not an function it’s an operator.
- Inside C Program we can calculate size of all data types including user defined and in built data types.
- However Sizeof Operator cannot calculate Size when we pass function name as parameter.
- 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 :
- &main will gives us Address where we have defined the actual function.
- sizeof operator will treat address as “integer” data type.
- In the 32 bit compiler, Size of Integer = 2 bytes (in Turbo C/C++ Compiler), therefor output is 4.
- Requirement of the memory for storing the integer data type will vary from from compiler to compiler.
- Now we have main function at any random memory location say 1000 then -
&main; = 1000
- 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.
- We cannot compute the sizeof the function by passing the function name of address of function as parameter to sizeof operator.
Consider the below program -
Must Read : Sizeof Pointer | Nested Sizeof | Size of void pointer