sizeof(x++)가 x를 증가시키지 않습니다
조회수 2240회
발생하는 문제 및 실행환경
밑에 코드를 실행하면 x
가 6으로 증가돼서 4와 6
이 나올 줄 알았더니
윈도우에서 dev c++ 로 출력해보니 4와 5
가 나왔습니다.
왜 sizeof(x++)
에서 증가시켜줬는데 x값이 그래도 5인 거죠?
소스코드
#include <stdio.h>
int main() {
int x = 5;
printf("%d와 ", sizeof(x++));
printf("%d\n", x);
return 0;
}
출력 : 4와 5
1 답변
-
C99 Standard의 6.5.3.4/2에서는
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.
라고 합니다.
sizeof()
는 피연산자로exression
이나type
의 이름(int
,char
등)이 올 수 있고, 결과는 피연산자 타입의 크기를 return하는데이때 피연산자가 variable length array type일 때만 피연산자를 계산(evaluate)합니다. 즉, x는 int형으로 이에 해당하지 않기 때문에 x++는 실행되지 않습니다.
댓글 입력