-4

I am currently preparing for finals as we speak. I am unable to figure out the answer to this one and why it would be that answer. Would anybody be able to explain to me the answer and how it resulted in that? This is from an old exam. Thank you in advance!

int main(){
   int i = 1, j=1, k=1;
   printf("%d", ++i || ++j && ++k);
   printf("%d %d %d \n", i, j, k);
   return 0;
}
Cole
  • 51
  • 9
  • 1
    What is your understanding when it comes to the precedence of logical operators and increment operators and on what do you base this? *Hint:* Check out the C standards document for authoritative information. – David Hoelzer May 06 '16 at 03:02
  • 2
    What does printf have to do with it? The same precedence rules apply as usual -- the increments first, then logical AND, finally logical OR. – Dan Mašek May 06 '16 at 03:02
  • 1
    For exam preparation, you can try what it will print. That way, you will learn more. ;) – Ian May 06 '16 at 03:05
  • @DanMašek I am sort of confused on what the AND and OR operators actually do within a printf statement. J will be 2 and K will be 2. Then, it will be 2 && 2. What exactly would be the answer to that? I don't get it. – Cole May 06 '16 at 03:06
  • @Ian I ran it and got a different result then my Professor wrote down. Making me even more confused. – Cole May 06 '16 at 03:06
  • @Cole ah, then you should post what your professor wrote and what you actually get. – Ian May 06 '16 at 03:07
  • Replace the line `printf("%d", ++i || ++j && ++k);` by `int r = ++i || ++j && ++k; printf("%d", r);`. Maybe that will help. – R Sahu May 06 '16 at 03:07
  • @Cole -- there's an expression `++i || ++j && ++k` that determines the value of the second printf argument. Additionally, a zero value is considered false, any non-zero is considered true. – Dan Mašek May 06 '16 at 03:08
  • @Ian My Professor wrote 2??? When I ran it I got: 12 1 1 – Cole May 06 '16 at 03:09
  • @DanMašek I am sorry, but I am extremely confused on what the expression actually does. I don't get what the && and || will do?! – Cole May 06 '16 at 03:11
  • 1
    Basically, this is a roundabout way of asking how many components of three-part expression will be evaluated. Once you know the answer to that, you would know what gets printed. – Sergey Kalinichenko May 06 '16 at 03:12
  • @dasblinkenlight It will evaluate the ++i which is greater than 0 making it true. So it will not evaluate the other side of the expression because it is || not &&. So it will then print out the 2 from the i? So it will print out 2. Then the next print statement will print 2 1 1. So in total it will be 2 2 1 1 ? – Cole May 06 '16 at 03:15
  • @Cole No, logical expressions can print only 0 or 1; no other values are possible. `i` would be 2, so that's what is going to get printed on the next line. – Sergey Kalinichenko May 06 '16 at 03:17
  • First rule for a beginner: trust your compiler. (Zero'ed rule is to enable warnings, but that is a different subject). So you see what you get and now figure out **why** you get it. This is simple, as there are books and other information around nowadays. – too honest for this site May 06 '16 at 03:20
  • @dasblinkenlight Ah, I figured it out. The && equals to "true" or 1. Which makes the || true. This results in the % d printing out 1 or "true". Then the next printf will print out the values of i, j, k. Which are 2 1 1, respectively. So final result is 1 2 1 1. My only question is, why did the value of i change, but not the value of j or k. Also thank you all for your help! – Cole May 06 '16 at 03:22
  • @Cole The value of an increment/decrement expression changes only when it gets evaluated. If `j++` does not get evaluated, the value of `j` does not change. – Sergey Kalinichenko May 06 '16 at 03:25
  • @dasblinkenlight Okay, that makes sense, but as explained to me before the && takes precedence over the ||, so wouldn't the && run first making ++j and ++k get evaluated? – Cole May 06 '16 at 03:27
  • @Cole That's why neither `++j` nor `++k` get evaluated. `++i` gets evaluated first, and the expression stops evaluating. – Sergey Kalinichenko May 06 '16 at 03:32
  • @dasblinkenlight But if the && takes precedence, why would the ++i get evaluated first? – Cole May 06 '16 at 03:34
  • @Cole That's due to [short circuit evaluation](https://en.wikipedia.org/wiki/Short-circuit_evaluation). – user3386109 May 06 '16 at 03:48
  • 1
    @Cole You're confused between **order of evaluation** and **operator precedence**. – Spikatrix May 06 '16 at 04:10

1 Answers1

1

The program is a very simple example to understand the precedence, if you don't know the operator precedence look here. From my understanding this is what I think, let's take it step by step(following left to right association):

  1. ++ wins over everything, i is increamented:

    int main(){
       int i = 1, j=1, k=1;
       printf("%d\n", ++i );
       printf("%d %d %d \n", i, j, k);
       return 0;
    }
    

Output:

2
2 1 1 
  1. Here comes the logical operator, now you need to know in case of some expression like ( expr1 || expr2), expr2 gets executed when expr1 is zero or false, here i=2 hence it won't. Also, first printf prints true(1) or false(0) after executing ( expr1 || expr2)

    int main(){
       int i = 1, j=1, k=1;
       printf("%d\n", ++i || ++j);
       printf("%d %d %d \n", i, j, k);
       return 0;
    }
    

Output:

1
2 1 1
  1. Finally, in case of ( expr1 && expr2 ) expr2 only gets executed when expr1 is true or 1, in this case the expr1 is not true hence ++k won't increament. Also following the same above rule for first printf.

    int main(){
       int i = 1, j=1, k=1;
       printf("%d\n", ++i || ++j && ++k);
       printf("%d %d %d \n", i, j, k);
       return 0;
    }
    

Output:

1
2 1 1

Please see following references as well:

Order of execution for an if with multiple conditionals

Order of logical OR execution in C

Community
  • 1
  • 1
PseudoAj
  • 5,234
  • 2
  • 17
  • 37