Variable assignment inside if( here ) while comparing

When I do this:

if (i_a=3)

I usually want to do

if (i_a==3)

Common mistake, I suppose.

But sometimes it makes sense something like this:


Where test() is just

int test(){
  return 5;

looks like i_a becomes 0 ou 1 (false ou true?) according to the == result.

But :slight_smile: if I do


It seems to do both things: assigns test() return to i_a and compares test() return (or i_a) to 3.

I have done several tests and it seems to work smoothly.

Do this make sense?



The result of the assignment “i_a = test ()” will evaluate to either true (non-zero) or false (zero).
Neither true nor false will be equal to 3

Yes, I'll use parenthesis rather than remember the more arcane Rules of Precedence. It makes it at least appear that you've thought about it.

You can also look at Assignment operators | Microsoft Docs under “Result of Assignment Operators” for another point of view.


Is equivalent to:
if(i_a=( test()==3) )

So this statement is correct:
“looks like i_a becomes 0 ou 1 (false ou true?) according to the == result.”

THX @6V6gt. While I was over getting my first vaccine shot I kept struggling with all these cases. I wrote this on an online C/C++ compiler. Handy for no Arduino attached.

# include <stdio.h>

int i_a;

int test()
    return 3;

int main()
    printf("Hello World\n");
    if ( i_a = test()  == 3 )    printf("\n (  =   ==  )   is true, i_a is %d\n", i_a);

    if ( i_a = (test() == 3))    printf("\n (  = ( == ))   is true, i_a is %d\n", i_a);

    if ((i_a = test()) == 3 )    printf("\n (( = ) ==  )   is true, i_a is %d\n", i_a);

    return 0;

All three expressions are true if test returns 3. All are false otherwise. The appearance at the bottom of the precedence chart of the assignment operators spun me around a bit but explains it of course. If you add the fact that the assignment statement returns the assigned value. Then add right-to-left and you can figure how things like

a = b = c;

are working.

I also use ( ) as mentioned earlier either to make sure I get what I want or as a courtesy to those who might read my code or even myself one day later. Although it would be nice to have a brain with sufficient number of cooperating cells that could just confidently omit any unnecessary parentheses.


Hello World

 (  =   ==  )   is true, i_a is 1

 (  = ( == ))   is true, i_a is 1

 (( = ) ==  )   is true, i_a is 3

...Program finished with exit code
Press ENTER to exit console.


I tend also to use parentheses to add clarity to a compound expression and usually try to steer clear of relying on mysterious side effects of action. An “if” condition statement is not really the best place to pack in a load of assignments.

I will sometimes do this, though, to manage the wrap around and increment of a loop counter variable, say when handling the multiplexing of a display:

if ( ++i >= arraySize ) i = 0 ;

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.