I thought x
would increase to 6 if I run the code below, so 4 and 6
would come out
When I printed it out as dev c++ in the window, 4 and 5
came out.
Why is the x value still 5 when sizeof(x++)
increased?
#include <stdio.h>
int main() {
int x = 5;
printf("%d and ", sizeof(x++)));
printf("%d\n", x);
return 0;
}
Output: 4 and 5
In 6.5.3.4/2 of C99 Standard,
The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant.
That's what it says.
sizeof()
may be an operand with the name of exression
or type
(int
, char
, etc.)
The result returns the size of the operand type
At this time, calculate the operand only when it is of variable length array type. In other words, x is int type and does not correspond to it, so x++ does not run.
© 2024 OneMinuteCode. All rights reserved.