In the case that you've presented here, sizeof(a) is resolved during compile. The only time it's resolved during runtime is when you're dealing with C99's variable length arrays.
The reason the above code works is that the number of bytes taken up by an int is always going to be the same, no matter what number is being stored in it. If you're compiling for a system where sizeof(int) is four bytes, then it's going to be four bytes no matter what you do to it.
@tux4life thank you for your links but I have a different doubt...I want to know that since the sizeof is compile time operator then why does thee above code outputs 4 even though the call to foo() will be made at runtime..So how does the compiler know that the value passed as parameter to sizeof will be int if the call itself is made at runtime!!!
I guess it is because the size of the return type of foo() is known at compile time, but I'm probably NOT correct on this.
I hope that someone else can clarify on this, I might learn something too then ;)
Okay, apparently I was wrong
Standard C (ISO/IEC 9899:1999) section 22.214.171.124:
"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."
foo() on line 4 of that code is a pointer, not a function. All pointers regardless of type are the same size, and in 32-bit compilers they are 4 bytes. szieof() does not work on functions as it treats function names as pointers to a function.
I want to know that since the sizeof is compile time operator then why does thee above code outputs 4 even though the call to foo() will be made at runtime.
You'll notice that foo() isn't called when used as an operand to sizeof:
That's because sizeof only evaluates the result of an expression, even if the expression contains runtime-only components or would be completely invalid if actually run. Another example is with malloc():
int *p = malloc(sizeof *p);
Dereferencing an uninitialized pointer is a horrible idea, but because sizeof evaluates the result as if the expression were run instead of actually running it, the above line is completely safe and even recommended.
@ancient dragon thankyou for your answer>> i tried the code by replacing the return type of foo() with float then also the output was 4..So this proves that foo() in sizeof behaves as function pointer..
i tried the code by replacing the return type of foo() with float then also the output was 4..So this proves that foo() in sizeof behaves as function pointer..
That proves nothing as the most common size of float is 4 bytes and there's no guarantee that a function pointer have the same size as object pointers. Try using double and you'll probably get output of 8, or char and you'll get output of 1. sizeof in this case is evaluating to the type of the return value.
I tried the code by replacing the return type of foo() with float then also the output was 4..So this proves that foo() in sizeof behaves as function pointer..please contribute if you feel my conclusion is wrong!!