3
 void calculate(){

  int x=3, y=3, z=1;

  printf("%d\n",z+=x<y ? 10:20 );

 }

The above code prints 21.

I understand that first, the program will evaluate x < y => 0, then z = z + 0 = 1, shouldnt the program prints 10 because 1 is another form of true.

The program clearly picked the second option, and it also increments it to 21, could someone please explain?

mopodafordeya
  • 635
  • 1
  • 9
  • 26

2 Answers2

12

The conditional operator has higher precedence than the assignment operator.

z+=x<y ? 10:20

is evaluated as

z+= (x<y ? 10:20)

You can get a summary of C operators precedence here:

http://www.kernel.org/doc/man-pages/online/pages/man7/operator.7.html

ouah
  • 142,963
  • 15
  • 272
  • 331
  • -1 for disinformation (I can't downvote though). The conditional operator doesn't have higher precedence than assignment. That's why int x=3,y=5;x – kotlomoy May 18 '13 at 12:37
  • http://stackoverflow.com/questions/7499400/c-ternary-conditional-and-assignment-operator-precedence – kotlomoy May 18 '13 at 12:43
  • 2
    @kotlomoy First, you don't understand what the word "disinformation" means, second you don't understand your own citation. Did you even bother to read ouah's citation, or the language standard, or any other source on C precedence, all of which confirm that the conditional operator has higher precedence than assignment? – Jim Balter May 18 '13 at 12:56
  • @Jim Batler read the link. I'll give one quote for example: "Assignment has the same priority as conditional in the table and associativity is right to left, resulting in assignment be done correctly before the conditional." – kotlomoy May 18 '13 at 13:08
  • @kotlomoy In the C Standard the precedence derives from syntax but in K&R 2 you can read a quotation similar to mine: *the precedence of ?: is very low, just above assignment* (in an example similar to this one btw) – ouah May 18 '13 at 13:08
  • 2
    Note that kotlomoy's link is to a C++ question, but this is a C question. Notably, you can't have ternary lvalue expressions in C. – Jim Balter May 18 '13 at 13:23
  • @Jim Batler If you have any arguments, give it then. Otherwise this dialog is not constructive. You can start by explaining what's wrong with my example code. Just calling me stupid is not good enough argument for me. – kotlomoy May 18 '13 at 13:28
  • @kotlomoy the syntax rules from C and C++ are different. In C your example `x – ouah May 18 '13 at 13:31
  • @kotlomoy No one called you stupid, but indeed you are being non-constructive, from your pseudo -1 on. – Jim Balter May 18 '13 at 13:31
  • @Jim Balter at last some answer. So ternary has different precedence in C and C++? I cancel my -1 vote (which I didn't give anyway) – kotlomoy May 18 '13 at 13:33
  • @JimBalter despite being both invalid I'm affraid it is equivalent – ouah May 18 '13 at 13:41
  • Sorry, I missed that you said "in C" ... I was distracted by the crank. – Jim Balter May 18 '13 at 13:48
3

First it evaluates x less than y. Since x = 3 and y = 3, this is false, so it evaluates to 20.

Since z already = 1, you're adding 20 to it:

z += 20

z = z + 20

z = 1 + 20

johnchen902
  • 9,531
  • 1
  • 27
  • 69
Jen
  • 41
  • 2