문제

I am trying to optimize some code and I was wondering if the return value of a condition like

(1>0)

is always 1 in c99 ? I couldn't find the answer on the web and the few tests I made with gcc seems to indicate this is true. But is this part of the langage specification ?

The exact code (marching squares algorithm) :

to_run->data[(y / 2) * (my_grid->width / 2) + (x / 2)] =
    (up[0] > level) +
    (up[1] > level) << 1 +
    (down[0] > level) << 2 +
    (down[1] > level) << 3;
도움이 되었습니까?

해결책

Yes.

C99 §6.5.8 Relational operators

Each of the operators < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to) shall yield 1 if the specified relation is true and 0 if it is false. The result has type int

다른 팁

Yes.

The draft C99 spec says:

Each of the operators < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to) shall yield 1 if the specified relation is true and 0 if it is false. The result has type int.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top