最近和一个朋友去混他们的计算机研究生基础课C++。老师讲课的时候讲了个:
num++ equal to num = num +1
++num equal to num = num +1;
num =1;
n= num++ *3; // 3
n = ++num * 3; //6
老师一两句就带过去了,我想了半天都没有想清楚,后来google 了一下, 发现有个人的解释特别详细:
let's say we have num=5
if we say
cout<<++num;
it's the same thing as writing
num += 1;
cout<<num;
and the output will be 6
but if we say
cout<<num++;
it's the same thing as writing
cout<<num;
num += 1;
and the output will be 5
by the way, num += 1; means num = num + 1; and it works for any operator (eg. num *= 2; multiplies num by 2)
a return value is what a function returns... ++ is a function too,
老师一两句就带过去了,我想了半天都没有想清楚,后来google 了一下, 发现有个人的解释特别详细:
let's say we have num=5
if we say
cout<<++num;
it's the same thing as writing
num += 1;
cout<<num;
and the output will be 6
but if we say
cout<<num++;
it's the same thing as writing
cout<<num;
num += 1;
and the output will be 5
by the way, num += 1; means num = num + 1; and it works for any operator (eg. num *= 2; multiplies num by 2)
a return value is what a function returns... ++ is a function too,