j = ++ i + ++ i; [英] j= ++i + ++i ;
问题描述
j的输出。在下面的代码中是8.有人能解释一下
概念吗?
#include< stdio.h>
#包括< conio.h>
int main(无效)
{
int i = 2;
int j = 0;
j = ++ i + ++ i;
printf("%d%d",i,j);
getch();
}
The output of "j" in the below code is 8. Can anybody explain me the
concept?
#include<stdio.h>
#include<conio.h>
int main(void)
{
int i=2;
int j=0;
j= ++i + ++i ;
printf("%d %d", i, j);
getch();
}
推荐答案
Rajat< ra ** ******@gmail.comwrote:
Rajat <ra********@gmail.comwrote:
j的输出在下面的代码是8.任何人都可以解释一下
的概念吗?
The output of "j" in the below code is 8. Can anybody explain me the
concept?
是的:首先阅读常见问题解答有帮助。
http://c-faq.com/expr/evalorder1.html
阅读3.1,3.2,和3.3。
-
C. Benson Manica |我*应该*知道我在说什么 - 如果我
cbmanica(at)gmail.com |不,我需要知道。火焰欢迎。
Yes: Reading the FAQ first helps.
http://c-faq.com/expr/evalorder1.html
Read 3.1, 3.2, and 3.3.
--
C. Benson Manica | I *should* know what I''m talking about - if I
cbmanica(at)gmail.com | don''t, I need to know. Flames welcome.
Rajat写道:
Rajat wrote:
>
输出" J型在下面的代码是8.任何人都可以解释我
的概念吗?
#include< stdio.h>
#包括< conio.h>
int main(无效)
{
int i = 2;
int j = 0;
j = ++ i + ++ i;
printf("%d%d",i,j);
getch();
}
>
The output of "j" in the below code is 8. Can anybody explain me
the concept?
#include<stdio.h>
#include<conio.h>
int main(void)
{
int i=2;
int j=0;
j= ++i + ++i ;
printf("%d %d", i, j);
getch();
}
如果没有getch()的来源,我们无法分辨。另外还有
没有像< conio.hin标准c这样的标题。此外,您的主要人员无法返回值
。此外,对j的赋值调用
未定义的行为。一个杰出的成就,很多错误在
这么短的节目。
-
Chuck F(cbfalconer at maineline dot net)
适用于咨询/临时嵌入式和系统。
< http://cbfalconer.home.att.net>
Without the source of getch() we cannot tell. In addition there is
no such header as <conio.hin standard c. Also, your main fails
to return a value. Further yet, the assignment to j invokes
undefined behavior. An outstanding achievement, so many errors in
so short a program.
--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Rajat写道:
Rajat wrote:
j的输出在下面的代码是8.任何人都可以解释一下
的概念吗?
The output of "j" in the below code is 8. Can anybody explain me the
concept?
是。
之后:
Yes.
After:
int i = 2;
int j = 0;
int i = 2;
int j = 0;
行:
the line:
j = ++ i + ++ i;
j= ++i + ++i ;
并不意味着什么。 (详见常见问题解答。)因此实施
可以做任何自然的事情。在这里,自然而然的是
看起来像两次递增`i`,这是你写的,
得到4,然后将`4`加到`4`到得到'8`。但是另一个实现
可以增加`i` / once /并将`3`添加到`3`以获得`6`。
[你问/为什么/它可以选择实施`i`一次?嗯,'++ i`
意味着交付i + 1并在方便的时刻将该值存储在i中
在下一个序列点[1]之前。因为它是未定义的行为,所以
在下一个序列点之前两次更新相同的变量,并且
编译器可以假设你,All-Knowing Programmer,
在您的代码中不会有未定义的行为,它只需要
记住我必须增加`i`。第二个`++ i`记得同样的
。所以`i`在一个方便的时刻只增加一次,
说在添加后发生。]
另一个实现可能会认为这句话是生成
未定义的行为并生成诊断消息和代码
相当于`j = 17`(或`42`或`0`或`-1`或`MAX_INT`或...)
另一个实现在一台机器上运行,增加两个
指令:
ib Ra ,Rb ;;; Ra + = 1,Rb + = 1
但是由于没有人会使用同一个寄存器的ib(因为
你可以使用`inc Rx, #2`),`ib Rx,Rx`
的操作代码表示`Rx:= 0`。由于C编译器可以假设两次更新同一变量的
未定义行为不能合法地发生,因此它会将您的代码编译为:
添加Rj,Ri,Ri
添加Rj,Rj,#2
ib Ri,Ri
`j`得到你可能期望的值,但是`i`是
意外地0 ...
[1]这里的顺序要点在声明的最后;你可以
把它想象成用分号标记。
-
Chris" Perikles胜利 Dollin
- 在严格监督下在实验室出生 - , - Magenta,/ Genetesis /
doesn''t mean anything. (See the FAQ for details.) So the implementation
can do whatever comes naturally to it. Here, what comes naturally
looks like incrementing `i` twice, which is what you wrote, which
got 4, and then adding `4` to `4` to get `8`. But another implementation
could increment `i` /once/ and add `3` to `3` to get `6`.
[You ask /why/ it could choose to implement `i` once? Well, `++i`
means "deliver i+1 and store that value in i at a convenient moment
before the next sequence point [1]". Since it''s Undefined Behaviour to
update the same variable twice before the next sequence point, and
the compiler is allowed to assume you, the All-Knowing Programmer,
won''t have an Undefined Behaviour in your code, it only needs to
remember "I must increment `i`". The second `++i` remembers the same
thing. So `i` gets incremented only once, at a convenient moment,
say after the additions happen.]
Another implementation might recognise this statement as producing
Undefined Behaviour and generate a diagnostic message and code
equivalent to `j = 17` (or `42` or `0` or `-1` or `MAX_INT` or ...)
Another implementation runs on a machine with a Increment Both
instruction:
ib Ra, Rb ;;; Ra += 1, Rb += 1
However since no-one would use ib with the same register (since
you can use `inc Rx, #2`), the operation code for `ib Rx, Rx`
means `Rx := 0`. Since the C compiler can assume that the
undefined behaviour of updating the same variable twice can''t
legally happen, it compiles your code as:
add Rj, Ri, Ri
add Rj, Rj, #2
ib Ri, Ri
`j` gets the value you might have been expecting, but `i` is
unexpectedly 0 ...
[1] Here the sequence point is at the end of the statement; you can
think of it as being marked by the semicolon.
--
Chris "Perikles triumphant" Dollin
"- born in the lab under strict supervision -", - Magenta, /Genetesis/
这篇关于j = ++ i + ++ i;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!