I was asked a very interesting question by a friend, he gave me the following code and asked me, to explain the output of the code,
4 2 ( in case of a 32 bit compiler)
2 2 ( in case of a 16 bit compiler like turbo c)
The sizeof operator does not evaluate the expression inside it rather it simply considers the type of the expression inside it . The sizeof is a compile time operation, so it is evaluated at compile time, not at run time.
The compiler reads sizeof(++a) at the compile time, as sizeof(int) and therefore, it gives the value to the statement, sizeof(int) as 4.
There is no evaluation of the statement ++a, as sizeof(++a) is evaluated at the compile time as sizeof(int) . Therefore, there is no change in the value of a, so, the output of printf(“%d”,a); is 2, not 3.
THE FINAL CONCLUSION:
The main point to remember is that sizeof(expression) is read by the compiler as sizeof(type of the expression) at the compile time and the operator sizeof is evaluated at the compile time.
Therefore, the expression is not evaluated .
Anyone would like to contribute any other point, or make the answer better by suggesting any corrections?