sizeof(x++)가 x를 증가시키지 않습니다

조회수 2229회

발생하는 문제 및 실행환경

밑에 코드를 실행하면 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 답변

  • 좋아요

    0

    싫어요
    채택 취소하기

    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++는 실행되지 않습니다.

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)