Is the behavior of if (++ i) ++ i defined?

Suppose I have this code:

int main(void)
{
    int i = rand();
    if (++i) ++i;
    return i;
}

Is the behavior defined here? I know that it i = ++iis undefined, and the second line in maincontains a similar thing. The challenge rand()is to stop the compiler from optimizing what, in my opinion, is the breaking line.

+6
source share
3 answers

From C standard

The following are the sequence points described in: -

.... , . : , (6.7.9); (6.8.3); (if switch) (6.8.4); while do (6.8.5); () for (6.8.5.3); () return.

, , . , , -.

+7

, .

++i if.

+5

.

First, the condition is evaluated if. There is a side effect of the increment i, then the increment is iused as a control expression if. In case this value is true , there is a sequence point before evaluating the operator in the body ifthat is located ++i.

+3
source

All Articles