-1

Hi just wondering if you use a chained assigment in an if condition, would the leftmost variable be used to check the if condition

like a=b=c , its a thats ultimetly checked and not b or c

#include <stdio.h>

 int main()
 {
  int a, b, c =0;
 
  // does this reduce to a == 100 and the variables b or c are not checked if they are == to 100 but simply assigned the value of 100 ? 
  if( (a = b = c = 100) == 100)
     printf( "a is 100 \n");

   
 return 0;
}
visitor
  • 111
  • 5

2 Answers2

4

The expression is not actually checking a or b or c.

An assignment expression, like any expression, has a value. And in this case it is the value that is stored. However, the actual storing of the value in an object is a side effect so there's no guarantee that it has happened at the time the comparison operator is evaluated.

So the condition is actually more like:

if (100 == 100)

With the assignment to a, b, and c happening in a manner that is unsequenced with respect to the comparison.

This is spelled out in section 6.5.16p3 of the C standard regarding assignment operators:

An assignment operator stores a value in the object designated by the left operand. An assignment expression has the value of the left operand after the assignment, but is not an lvalue. The type of an assignment expression is the type the left operand would have after lvalue conversion. The side effect of updating the stored value of the left operand is sequenced after the value computations of the left and right operands. The evaluations of the operands are unsequenced.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
dbush
  • 205,898
  • 23
  • 218
  • 273
  • thx, so i guess one should not use chained assignments if there unpredictable – visitor Jan 31 '21 at 18:48
  • 2
    @AfzelAdam It's not unpredictable by itself. What you can't do is read and write a variable in the same expression without an intervening sequence point. In your code you're only writing so there's no problem. – dbush Jan 31 '21 at 18:49
  • @AfzelAdam *thx, so i guess one should not use chained assignments if there unpredictable* No, you don't use chained expressions like that because they're hard to read, hard to debug, and therefore much more likely to have bugs. Anyone telling you to reduce the number of lines in your program by cramming more and more logic into one line is, frankly, not worth listening to. What's important with code is keeping it readable so you can easily see what's going on, and then debug it when you have to fix bugs in it. And you ***will*** have to fix bugs in it. – Andrew Henle Jan 31 '21 at 19:11
  • To clarify, you aren't saying that it's UB, right? – HolyBlackCat Jan 31 '21 at 19:15
  • 1
    @HolyBlackCat Correct, OP's code does *not* exhibit UB. – dbush Jan 31 '21 at 19:20
2

The condition is always true. Your code is equivalent to:

a = 100;
b = 100;
c = 100;
printf( "a is 100 \n");
HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
  • 1
    The code is better thought of as `c = 100; b = c; a = b; if (a == 100)…`, as each assignment evaluates to the result after conversion to the type of the left-hand operand, not the original value of the right-hand operator. E.g., for `float a; int b;`, `a = b = 3.5;` will set `a` to 3, not 3.5. – Eric Postpischil Jan 31 '21 at 18:41
  • But what is actually being tested to see if its 100, like is it the variable a – visitor Jan 31 '21 at 18:42
  • @AfzelAdam The final value of `a` is being tested, which is always `100` in your case. – HolyBlackCat Jan 31 '21 at 19:03