본문 바로가기

StackOverflow

[C] --> 오퍼레이터를 뭐라고 부르죠?

http://stackoverflow.com/questions/1642028/what-is-the-name-of-the-operator?rq=1


Q: --> 오퍼레이터를 뭐라고 부르죠?

아래와 같은 코드를 보았습니다. --> 오퍼레이터를 를 뭐라고 부르죠?

"while( x--> 0)"

(질문자: GManNickG)


A: 그거 오퍼레이터 아닙니다. 그냥 -- 연산을 한 뒤, 크기비교를 > 이 연산자로 한 것 뿐입니다.

(답변자: Potatoswatter)





After reading Hidden Features and Dark Corners of C++/STL on comp.lang.c++.moderated, I was completely surprised that the following snippet compiled and worked in both Visual Studio 2008 and G++ 4.4.

Here's the code:

#include <stdio.h>
int main()
{
    int x = 10;
    while (x --> 0) // x goes to 0
    {
        printf("%d ", x);
    }
}

I'd assume this is C, since it works in GCC as well. Where is this defined in the standard, and where has it come from?

shareeditflag




















4596down voteaccepted
+50

--> is not an operator. It is in fact two separate operators, -- and >.

The conditional's code decrements x, while returning x's original (not decremented) value, and then compares the original value with 0 using the > operator.

To better understand, the statement could be written as follows:

while( (x--) > 0 )
shareeditflag