what does x— or x++ do here?

后端 未结 7 1170
猫巷女王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:45

    x-- subtracts/decrements the value of x by one.

    Conversley x++ adds/increments by one.

    The plus or minus signs can either be before (--x) or after (x--) the variable name, prefix and postfix. If used in a expression the prefix will return the value after operation has been performed and the postfix will return the value before operation has been performed.

    int x = 0;
    int y = 0;
    y = ++x; // y=1, x=1
    
    int x = 0;
    int y = 0;
    y = x++;// y=0, x=1
    

提交回复
热议问题