0
    #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.

Stuart
  • 711
  • 1
  • 11
  • 35
nitesh.kodle123
  • 1,041
  • 4
  • 11
  • 23
  • 2
    @user2864740: There is no UB here, read it more carefully. – Ed S. May 15 '14 at 02:15
  • Possible duplicate of [Multiple logical operators in one line of code](https://stackoverflow.com/questions/35382271/multiple-logical-operators-in-one-line-of-code) – phuclv Aug 18 '18 at 14:50

3 Answers3

3

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)
Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • thanks for response this expression will be evaluated as (-1&&-1)&&(0||2) – nitesh.kodle123 May 15 '14 at 02:23
  • @nitesh.kodle123: No. `&&` has a higher precedence than `||`, so it's `((-1 && -1) && 0) || 2` – Ed S. May 15 '14 at 02:26
  • Thanks I got it.Can you suggest me link which explains precedence operator in c. – nitesh.kodle123 May 15 '14 at 02:33
  • @nitesh.kodle123: What about that confuses you? – Ed S. May 15 '14 at 02:46
  • again one confusion postfix (++/--)has higher precedence than prefix(++/--) in the (article)[ en.cppreference.com/w/cpp/language/operator_precedence] it is given.I know prefix has higher precedence than postfix please correct me if I am wrong. – nitesh.kodle123 May 15 '14 at 02:48
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/52718/discussion-between-nitesh-kodle123-and-ed-s) – nitesh.kodle123 May 15 '14 at 02:49
  • 1
    @nitesh.kodle123: I don't believe that you do "know" that because it is wrong. Postfix has a higher precedence than prefix. That doesn't change the *semantics* of postfix v prefix, it simply defines the order of evaluation. I'm sorry but I can't join chat right now – Ed S. May 15 '14 at 02:49
2

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.

keshlam
  • 7,931
  • 2
  • 19
  • 33
0

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)

Mestica
  • 1,489
  • 4
  • 23
  • 33