How does the increment operator work in an if statement?

前端 未结 7 1426
感情败类
感情败类 2021-01-13 14:16
    #include 

    int main()
    {
        int x = 0;

        if (x++)
            printf(\"true\\n\");
        else if (x == 1)
            printf(         


        
7条回答
  •  野的像风
    2021-01-13 14:58

    You yourself wrote: "x++ is post increment, this means that the value of x is used then it is incremented"

    Consider what that means:

    1. x is 0

    2. The expression is evaluated, 0 is false, so the expression is false.

    3. The post increment happens, changing x from 0 to 1.
      (After the expression was evaluated)

提交回复
热议问题