nitesh.kodle123
nitesh.kodle123

Reputation: 1033

Confusion with post increment and logical operator?

    #include <stdio.h>
    #include <string.h>

    main()
    {
         int i=-1, j=-1, k=0, l=2,m;
         m = i++&&j++&&k++||l++;
         printf("%d%d%d%d%d", i, j, k, l, m);
    }

Output:

00131

I am confused how the expression is getting evaluated.

Upvotes: 0

Views: 677

Answers (3)

keshlam
keshlam

Reputation: 8058

I presume your question is about which increments will actually occur.

&& and || (logical and and logical or) are "short-circuit" operations. They evaluate only enough of their arguments to determine whether they're true or false. Effectively, x && y can be treated as x ? y : 0 and x || y can be treated as x ? 1 : y

&& takes precedence over ||, so start by looking at i++ && j++ && k++. This starts by evaluating i++, returning -1 and setting i to 0. Since the returned value is true (nonzero), we continue by evaluating j++, which once again returns -1 (true) and increments j to 0. We still haven't proven the value of the &&, so we evaluate k++, which returns 0 (false) and increments k to 1. That false gives us a final anded value of false.

Now we proceed to the ||. Effectively, you now have false || l++. The false is not enough to determine the result of the or, so we evaluate l++. That returns 2 (true), while setting l to 3. That true forces the value of the ||, and the final value of the expression, to be true.

Note that if i, j, or k had started as 0 (false), the later increments would not have occurred, since short-circuit evaluation would have decided they weren't needed in order to produce a result. In general, mixing && or || with side effects is a bad idea for exactly this reason -- it produces logic that is needlessly hard to understand. The ?: versions -- or a real if/then/else statement -- would make this interaction much, much clearer. You should understand how to read this sort of mess, because you will run into it in other programmers' C code -- but you should almost never write it. And if you must, you should document it to death. The sanity you save may be your own.

Upvotes: 2

Ed Swangren
Ed Swangren

Reputation: 124682

All that really matters here is the ||. Since l++ evaluates to the boolean 1 (true), the entire statement is true. So m is 1, and the rest are just their original values plus one for the post increment after they are evaluated.

You're evaluating the boolean expression:

((-1 && -1) && 0) || 2

As an aside, your definition of main should be:

int main(void)

Upvotes: 3

Mestica
Mestica

Reputation: 1529

use this rules:

i++ =post increments the value of i, k++ =post increments the value of k, l++ =post increments the value of l, j++ =post increments the value of j

&& - if values compared are both nonzero true(1) otherwise false(0)

|| - if values compared are both zero false(0) otherwise true(1)

then apply towards expression m (also read on operator precedence in c)

Upvotes: 0

Related Questions