2
#include<stdio.h>
int main(){
    int y=15;
    if(y++>19 && y++!=21 && y++>21)
    printf("%d",y);
    else
    printf("%d",y);
    return 0;
}

I expected output to be 15 or even 18 but it gives out 16 and I don't know why.

Christopher Moore
  • 15,626
  • 10
  • 42
  • 52
adii_turbo
  • 23
  • 3
  • 4
    Are you aware of the [short circuiting behavior of &&](https://stackoverflow.com/questions/628526/is-short-circuiting-logical-operators-mandated-and-evaluation-order)? – Brian61354270 Apr 16 '21 at 19:07
  • 2
    15 (rather than 16, because of postfix increment) is less than 19, not greater, so thanks to short-circuit evaluation the other operands don't need to be evaluated. You're still incrementing `y`, though, exactly one time - so `y` will be `16`. – Paul M. Apr 16 '21 at 19:07
  • ⟼Remember, it's always important, *especially* when learning and asking questions on Stack Overflow, to keep your code as organized as possible. [Consistent indentation](https://en.wikipedia.org/wiki/Indentation_style) helps communicate structure and, importantly, intent, which helps us navigate quickly to the root of the problem without spending a lot of time trying to decode what's going on. – tadman Apr 16 '21 at 19:19

4 Answers4

3

Here y++>19 ==> y is compared against 19 and then incremented due to postfix ++. So, y++>19 is actually evaluated as 15>19 and then y gets incremented and becomes 16.

Since 15>19 is false, the rest of the condition y++!=21 && y++>21 is not evaluated and control goes to else condition and prints y. Since y already became 16 above, it prints 16.

Note that in short-circuit evaluation, A && B && C, if A gets evaluated to true then only B gets evaluated. If both A and B gets evaluated to true then only C gets evaluated.

Thus, in your case, since A is false, there was no further evaluation of B and C and the control just enters else part of it and prints y.

2

I suggest to read about operator precedences (and while at it associativity). Usually each language has a specification that explains the functionings, so does C. Sources online are abound, a great book for learning C is K&R, well explained and interesting exercices.

I'll walk you through this example:

int y=15; // y is assigned the value 15, the return of the assignment is discarded (you will encounter that fact sooner or later surely)

The if statement can be described as (y++>19) && (y++!=21) && (y++>21) to make it clear. The && operator is short-circuited: i.e. as soon as the final result can be determined, the rest will not be evaluated. Concretely in this case, as soon as a false or 0 is seen it stops evaluating and return false.

y++>19 has two operators, postfix increment (higher precedence) and greater than (lower precedence). The postfix increment returns the value first, and then increments it, so effectively 15>19 is evaluated and y is incremented to 16. Now remember the short-circuit. The program stops evaluating that expression since the final result will not change. The program enters the else part and prints the value assigned to y which is 16.

Ely
  • 10,860
  • 4
  • 43
  • 64
  • Right @Ely he needs to properly learn assignment and logical operators before writing such statements since: false AND true results directly in a boolean false. – Manifest Man Apr 16 '21 at 19:46
2

Several things are going on in the line

if(y++>19 && y++!=21 && y++>21)

The && operator (along with the ||, ?:, and comma operators) forces left-to-right evaluation, meaning that y++ > 19 is evaluated before y++ != 21, which will be evaluated before y++ > 21. It also introduces a sequence point, so all side effects of y++ > 19 will be applied before y++ != 21 is evaluated.

The && operator short-circuits - y++ != 21 will only be evaluated if y++ > 19 evaluates to non-zero (true), and y++ > 21 will only be evaluated if both prior expressions evaluate to non-zero.

The postfix ++ operator yields the current value of the operand (so y++ evaluates to 15); as a side effect, it increments the operand, so after y++ > 19 is evaluated, y is incremented to 16, even though the overall expression evaluates to false (0).

John Bode
  • 119,563
  • 19
  • 122
  • 198
0

here is what you wrote but just a little bit shorter and faster:

#include<stdio.h>
int main(){
    int y = 15;
    y++;    // y = y + 1; means value of variable y equals 16 at this point
    if(y != 21 && y > 19)  // now you check this 16 != 21 && 16 > 19 which is false 
    printf("%d", y);
    else   // continue here directly 
    printf("%d", y);  // print value of variable y
    return 0;   // exit success
}

Yes, the result is always 16. If this is not what you expected, then make sure you correct your first statement if(y != 21 && y > 19) .

I hope, this will help you correct your IF-statement (my suggestion).

I can recommend you these following two videos to properly understand assignments, logical and comparison operators before writing such statements since:

false AND true -> false
true AND false -> false

(1) ArithmeticOperator Program https://www.youtube.com/watch?v=oaO2eT46Mm8

(2) ComparisonOperator Program https://www.youtube.com/watch?v=ovKVOj3xNsA

(3) LogicalOperator Program https://www.youtube.com/watch?v=kclf0PQnxrI

Upvote the answer after correction done.

Manifest Man
  • 873
  • 10
  • 16