what does x— or x++ do here?

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

    -- is the 'decrement' operator. It simply means that the variable it operates on (in this case the x variable) gets is decremented by 1.

    Basically it is shorthand for :

    x = x - 1;
    

    So what the code does :

    int x,y ; # Define two variables that will hold an integer
    x=7;      # Set variable X to value 7
    x-- ;     # Decrement x by one : so x equals 7 - 1 = 6
    y= x * 2; # Multiply x by two and set the result to the y variable: 6 times 2 equals 12
    x=3;      # set x to value 3 (I do not know why this is here).
    

提交回复
热议问题