Search code examples
cgeany

The value oF P is unexpected


please, How do you explain technically why P =2 and not P=3. (I tried it on Geany and it have the value 2).

int main()
{
    int N = 10, P = 5, Q = 10;
    N = 5;
    P = 2;
    Q = ++N == 3 && P++ != 3;
    printf ("N=%d P=%d Q=%d\n", N, P, Q);
    return 0;
}

thanks for your response.


Solution

  • Q = ++N == 3 && P++ != 3;
    

    The first expression (++N == 3) is false, thus the program never even executes the second expression. This phenomenon is called Short Circuit Evaluation