[英]Can someone tell me how does this work ? (C programming)
int m, n, j;
n=16;
j=15;
m = n++ -j+10;
printf("%d", m);
Output: 11. 输出:11。
Here, first, the old value of n
is given to m
and then it is incremented so the new value i get is 17 and then the expression is solved ie j+10
= 25 then the new value of n is subtracted by 25 ie 17-25. 在这里,首先,将
n
的旧值赋予m
,然后将其递增,以使新值i get为17,然后求解表达式,即j+10
= 25,然后将n的新值减去25,即17 -25。 Am i right ? 我对吗 ? but the answer doesn't match the output
11
. 但是答案与输出
11
不匹配。 Then how does this work ? 那这是怎么工作的呢? And also, i am new to programming and started learning C. Which book will you suggest is the best for me ?
而且,我是编程的新手,并且开始学习C。您会建议哪本书对我来说最好? As I've no programming experience.
由于我没有编程经验。 Thank you.
谢谢。
m = n++ -j+10;
is same as 与...相同
m = n -j+10;
n = n + 1; // m is 11.
If it was ++n
It would be 如果是
++n
n = n + 1;
m = n -j+10; //m is 12.
then the expression is solved ie j+10 = 25
然后求解表达式,即j + 10 = 25
No. It would be -j+10
= -5 不,这是
-j+10
= -5
My suggestion is, dont write complex expression unless you are completely sure what you are writing. 我的建议是, 除非您完全确定自己在写什么,否则不要写复杂的表达式。
You've got a few things wrong there. 您那里有些错误。
n++
will increment n
and return the original result, so you've then got m = 16 ...
. n++
将递增n
并返回原始结果,因此您得到了m = 16 ...
-j
so you've got m = 16 - 15 ...
. -j
所以m = 16 - 15 ...
-j
m = 16 - 15 ...
+10
so you've got m = 16 - 15 + 10
. +10
所以您得到m = 16 - 15 + 10
。
Now the last time I did maths that would come out as m = 11
like you're seeing. 现在我上一次进行数学运算时,就像您看到的那样,
m = 11
。
If you wanted it to be m = 17 - (15 + 10)
then you wanted: 如果您希望它是
m = 17 - (15 + 10)
那么您需要:
int m, n, j;
n=16;
j=15;
m = ++n -(j+10);
printf("%d", m);
in fact the post increment operation is done on n after the operation... you have 16-15+10 = 11 but if you print n you should have 17. 实际上,后递增运算是在运算后的n上完成的……您有16-15 + 10 = 11,但如果打印n则应该有17。
to begin, you can read some book on basics but this example is not simple; 首先,您可以阅读一些基础知识,但是这个示例并不简单; it include the precedence of operator which can be tricky.
它包括可能很棘手的运算符优先级。
begin simple... it's quite simple to write unreadable code in c. 开始简单...用c编写不可读的代码非常简单。 http://www.cs.cf.ac.uk/Dave/C/node4.html
http://www.cs.cf.ac.uk/Dave/C/node4.html
hope it helps 希望能帮助到你
In the expression m = n++ -j+10;
在表达式中
m = n++ -j+10;
The compiler treats the expression as m= n++ ((-j)+10)
编译器将表达式视为
m= n++ ((-j)+10)
As the intialized values of n and j are n = 16
and j = 15
. 由于n和j的初始值分别为
n = 16
和j = 15
。 We have m = 16++ ((-15)+10)
. 我们有
m = 16++ ((-15)+10)
。 We get output as 11
. 我们得到的输出为
11
。
After the expression is executed n
will be incremented. 执行表达式后,
n
将递增。
n++
first returns the value of n
and then increments it. n++
首先返回的值n
然后递增它。
so, the actual computation that takes place is m = 16 - 15 + 10
which is 11 因此,实际发生的计算是
m = 16 - 15 + 10
,即11
i think what you want is: 我想你想要的是:
m = (n+1) - (j+10);
the use of the ++
operator is to increment the value of n
for future use after you use it's current value to compute m
. 使用
++
运算符的目的是在使用n
的当前值来计算m
后增加n
的值以供将来使用。
for m
, first of all calculate n - j + 10
and assign it to m
. 对于
m
,首先计算n - j + 10
并将其分配给m
。 After that n++
is executed. 之后,执行
n++
。
at the end n = 17
, m = 11
最后
n = 17
, m = 11
You're making two incorrect interpretations. 您正在做出两种错误的解释。
Firstly, as indicated in other answers, n++
only increments n
after the entire expression has been evaluated. 首先,如其他答案所示,在对整个表达式求值之后,
n++
仅递增n
。
Secondly, you have -j+10
. 其次,您有
-j+10
。 This is not equal to -(j+10)
, so it is wrong to say that j+10
is 25
and you are looking a something - 25
. 这不等于
-(j+10)
,所以说j+10
为25
且您正在寻找something - 25
是错误的。 Another way of view -j+10
is 10-j
. 另一种查看
-j+10
是10-j
。
In answer to your question about a good book - you probably want to consider learning C++ instead of just plain old C, since C++ is a superset of C. And for C++ you need to get Bjarne Stroustrup's 'The C++ Programming Language'. 在回答有关一本好书的问题时,您可能想考虑学习C ++而不是普通的C语言,因为C ++是C的超集。对于C ++,您需要获得Bjarne Stroustrup的“ The C ++ Programming Language”。 It's easy enough to read and will last a long time on your bookshelf as a good reference.
它很容易阅读,可以在您的书架上长时间保存,作为一个很好的参考。
n
的值在以m
结尾的表达式中使用后, n
递增。
n++
is post incrementation . n++
是后递增 。 It only increments the value of n
after doing: m = n++ -j+10;
这样做之后,它只会递增
n
的值: m = n++ -j+10;
++n
is pre incrementation. ++n
是预递增。 It increments the value of n
before calculing m. 在计算m之前,它会增加
n
的值。 m = ++n -j+10;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.