Forum question address: http://topic.csdn.net/u/20091023/10/9e99cf60-ecbe-4011-8eb1-906111a82ddd.html? Seed = 1812802942 & R = 60636378 # r_60636378
Another example:
# Include <iostream>
Using namespace STD;
Int main ()
{
Int A = 1, B = 2;
Cout <sizeof (A = B)/sizeof (char) <Endl;
Cout <A <Endl;
System ("pause ");
Return 0;
}
Why is the result of a not 2?
Sizeof itself is a key word of C/C ++ (the same as int and char) rather than a function, which is destined to be executed during the compilation period, during compilation, A = B is not executed.
At the same time, sizeof is used to calculate the number of bytes of memory space occupied by a type, that is, sizeof (type name ). You may have said, either sizeof (2) orProgramThat's sizeof (A = B! Yes, this is acceptable, because during the compilation period, the compiler calculates the type of expression followed by sizeof, and then calculates the number of bytes occupied by this type of memory.
Well, back to our topic, why didn't A = B in sizeof (A = B) be executed? This is because the type of the value assignment expression is the same as the type of Its left value, which is the type of variable a here. Therefore, the compiler does not execute the = B operation, but simply regards sizeof (A = B) as sizeof (a). Here, a is of the int type, then it becomes sizeof (INT.