what does x— or x++ do here?

后端 未结 7 1167
猫巷女王i
猫巷女王i 2021-01-23 01:53

it is a silly Q for most of u - i know - but i one of the beginner here, and I can not understand why the output in here are 12 what does this (x--) do to the resu

7条回答
  •  伪装坚强ぢ
    2021-01-23 02:34

    x-- will decrement value of x by 1. It is a postfix decrement operator, --x is a prefix decrement operator.

    So, what's going on here?

     
    int x, y;    //initialize x and y
    x = 7;       //set x to value 7
    x--;         //x is decremented by 1, so it becomes 6
    y = x * 2;   //y becomes 6*2, therefore y becomes 12
    x = 3;       //x becomes 3

    By analogy, the ++ will increase a value by 1. It also has a prefix and postfix variant.

提交回复
热议问题