please help me understanding the following program?

int i= -1, j= 1, m;

m= i++ && j++ || i++;

printf("%d %d", i,m);

Recommended Answers

All 8 Replies

i isn't 0, so the first part of the logical AND succeeds. i is then incremented. Next j isn't 0, so the second part of the logical AND succeeds. j is incremented. The OR part of the test isn't performed due to short circuiting and m is set to a true value.

You can change how things work by making either i or j zero initially such that the AND part of the test fails. This will force the OR part of the condition to be executed. Changing i to zero will result in an output of "2 1" because i is now incremented twice.

thank you

You forgot the part about it being undefined behavior...
;o)

Member Avatar for I_m_rude
how is it undefined ? i is incremented but they are after 2 sequential points. How is it undefined. thanks to JAMES SIR in advance. 

Oops. Sorry, my mistake. It isn't undefined.

Oops. Sorry, my mistake. It isn't undefined.

That's why I forgot that it's undefined behavior. Because it isn't. ;)

How is it undefined.

As Walt said, it's not. At first glance one might make that mistake though, because there's a rule about modifying a single object more than once between sequence points being undefined. However, the || and && operators introduce a sequence point, so the statement is well defined. I myself gave it a double take to make sure everything was kosher before answering. ;)

thanks to JAMES SIR in advance.

...

And I learned something - another C confusion factor uncovered. Why are || and && sequence points? Rhetorical, no need to explain - I can see why they could be just not why they should be...

Why are || and && sequence points? Rhetorical, no need to explain - I can see why they could be just not why they should be...

Actually, that's a good question because it's not immediately obvious.

I'm sure you guessed that it's because of the short circuiting behavior. The operands are expected to be executed in a specific order, and a sequence point is required to enforce that order. If there weren't a sequence point for &&, for example, then the right hand expression couldn't expect the side effects of the left hand expression to have completed.

Is enforcing full completion of the expression necessary to make short circuiting work in all cases? To be honest, I'm having trouble thinking of an example that's practical in the real world, but that doesn't mean there aren't any.;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.