Question

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;
Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top