增量操作如何工作 [英] how do increment operations work

查看:60
本文介绍了增量操作如何工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



hi ..


我对增量运​​算符有一个简单的疑问..


可以任意一个告诉我到底发生了什么..


例如:

int i = 0,ans;


(ans = ++ i ++ i //这个

在编译时出错)


ans = ++ i + + i;

printf("%d%d",ans,i); //它打印2 1

i = 0;

ans = ++ i + ++ i;

printf(" ;%d%d,ans,i); //它打印4 2

i = 0;

ans = ++ i + ++ i + ++ i;

printf("%d%d",ans,i); //它打印7 3


4和7如何来解释任何一个...


谢谢

Pai


hi..

I have a simple doubt about increment operators..

Can any one tell me what is really happening..

Eg:
int i=0,ans;

( ans = ++i ++i // This
gives error while compiling )

ans = ++i + +i ;
printf(" %d %d ",ans,i); // it prints 2 1

i=0;
ans = ++i + ++i ;
printf(" %d %d ",ans,i); // it prints 4 2

i=0;
ans = ++i + ++i + ++i;
printf(" %d %d ",ans,i); // it prints 7 3

How do 4 and 7 came can any one explain...

Thanks
Pai

推荐答案

" pai" < GR **** @ yahoo.com>写道:
"pai" <gr****@yahoo.com> writes:
我对增量运​​算符有一个简单的疑问..


我认为你的意思是问题,而不是怀疑。你的英语方言中的单词可能是同义词

,但它们对大多数人来说意味着不同的东西

我们。

任何人都可以告诉我真正发生的事情..

例如:
int i = 0,ans;

(ans = ++ i ++ i //这个编译时出错)

ans = ++ i + + i;
printf("%d%d",ans,i); //它打印2 1

i = 0;
ans = ++ i + ++ i;
printf("%d%d",ans,i ); //它打印4 2

i = 0;
ans = ++ i + ++ i + ++ i;
printf("%d%d" ,ANS,ⅰ); //它打印7 3

4和7如何来解释任何一个......
I have a simple doubt about increment operators..
I think you mean "question", not "doubt". The words may be synonymous
in your dialect of English, but they mean different things to most of
us.
Can any one tell me what is really happening..

Eg:
int i=0,ans;

( ans = ++i ++i // This
gives error while compiling )

ans = ++i + +i ;
printf(" %d %d ",ans,i); // it prints 2 1

i=0;
ans = ++i + ++i ;
printf(" %d %d ",ans,i); // it prints 4 2

i=0;
ans = ++i + ++i + ++i;
printf(" %d %d ",ans,i); // it prints 7 3

How do 4 and 7 came can any one explain...




你的缩进过度且不一致,难以

阅读您的代码。


C FAQ位于< http://www.c-faq.com/>。阅读问题3.2。然后

阅读第3部分的其余部分。然后阅读其余的FAQ。


-

Keith Thompson(The_Other_Keith ) ks***@mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



Your indentation is excessive and inconsistent, making it difficult to
read your code.

The C FAQ is at <http://www.c-faq.com/>. Read question 3.2. Then
read the rest of section 3. Then read the rest of the FAQ.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


hi ..


抱歉没有清晰的英语方言。

我无法理解程序1中的增量如何工作

和程序2中为什么会出现编译错误。


这里是代码..


--------------------------------- ------

计划1

-------------------------- -----------

main(){


int i = 0,ans;


ans = ++ i + + i;

printf("%d%d \ n",ans,i);


i = 0;

ans = ++ i + ++ i;

printf("%d%d \ n",ans,i);


i = 0;

ans = ++ i + ++ i + ++ i;

printf("%d% d \ n,ans,i);


}


结果

2 1

4 2

7 3


-------------------- --------------------

program 2

------------ ------------- -----------------------

另一项计划是


main(){

int i = 0,ans;


ans = ++ i ++ i;

printf(" %d%d \ n");


}


这会产生编译错误

test1.c :4:在函数main中:

test1.c:4:错误:增量中的无效左值

test1.c:9:错误:语法错误之前 ; i


谢谢

Pai

hi..

Sorry for not having a clear english dialect.
I am not able to understand how is the increment works in program 1
and in program 2 why there is a compile error.

here is the code..

---------------------------------------
Program 1
-------------------------------------
main(){

int i=0,ans;

ans = ++i + +i ;
printf(" %d %d \n",ans,i);

i=0;
ans = ++i + ++i ;
printf(" %d %d \n",ans,i);

i=0;
ans = ++i + ++i + ++i;
printf(" %d %d \n",ans,i);

}

The result
2 1
4 2
7 3

----------------------------------------
program 2
------------------------------------------------
Another program is

main(){
int i=0,ans;

ans = ++i ++i ;
printf(" %d %d \n");

}

This gives compile error
test1.c:4: In function `main'':
test1.c:4: error: invalid lvalue in increment
test1.c:9: error: syntax error before "i"

Thanks
Pai


pai写道:
pai wrote:

嗨..

我对增量运​​算符有一个简单的疑问..

任何人都可以告诉我什么是真的正在发生..

例如:


您的代码不完整,并且非常难以阅读那么多

缩进(一个原因)可能在原始文件中使用TAB,这几乎总是一个坏主意。


你错过了:


#include< stdio.h>

#include< stdlib.h>


int main(void)

{

int i = 0,ans;


这些最好放在单独的线上,或至少水平间隔。

选择:


int i = 0,ans;





int i = 0;

int ans;

(ans = ++ i ++ i //
这个
在编译时出错)


这不是合法的C评论(也不是C ++风格的,至少对于
编译器而言。


当然这是语法错误。序列<表达1> < whitespace>

< expression2>在C中你是不合法的。你有'++ i`作为表达式1

和2.如果你期望不同的东西(我看不出那是什么)
可能是),请记住编译器遵循最大munch

规则,即它按顺序读取代码并尝试理解

最长的合法字符序列。在你的情况下,这些

都是`++ i',但将它们串在一起是没有意义的。

ans = ++ i + + i;
printf("%d%d",ans,i); //它打印2 1


这是未定义的行为。在评估表达式期间,可以随时执行`++ i`位

(但在序列点之前

标记为`;`在你的情况下,在上一个之后,在你的

案例中是前一个`;`)。对于表达式的第二部分取值

`i`也是如此(`+`中的`+`是一元加,

与数学中的+2完全相同,即没有什么比b $ b更多。你的编译器选择首先执行`++ i`然后取值

`i`,并将表达式计算为2.它可能决定这样做

其他方式,并给你0.

i = 0;
ans = ++ i + ++ i;
printf("%d%d", ANS,ⅰ); //它打印4 2


U.B.在这里,你的编译器选择保持一致,首先

做两个`++我',然后执行加法,然后给你4个结果。

i = 0;
ans = ++ i + ++ i + ++ i;


这里再次相同,但现在编译器显然决定先处理前两个`++ i',然后添加结果执行

第三个`++ i`,并将其添加到正在运行的总数中。


NB,所有这些只是我的双重猜测你的编译器决定在这个特殊的场合做
。下周它可能决定做一些完全不同的事情

。不要做这些事情(并且不要依赖于这个编译器切换到另一个时所做的事情。)

printf("%d%d", ANS,ⅰ); //它打印7 3


实际上很幸运,你从这个程序中得到任何东西,因为你

没有终止printf()' '\ n''强行冲洗stdout。


你也错过了这些:


返回0;

}

4和7如何来解释任何一个......

hi..

I have a simple doubt about increment operators..

Can any one tell me what is really happening..

Eg:
Your code is incomplete, and horribly difficult to read with that much
indentation (one reason may be using TABs in the original file, which
is almost always a bad idea).

You''re missing:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int i=0,ans;
These are better put on separate lines, or at least horizontally spaced.
Choose from:

int i = 0, ans;

and

int i = 0;
int ans;

( ans = ++i ++i //
This
gives error while compiling )
This is not a legal C comment (nor are C++ style ones, at least for on
compiler).

Of course this is a syntax error. Sequence <expression1> <whitespace>
<expression2> is not legal in C. You have `++i` as both expression 1
and 2. If you expected something different (and I can''t see what that
might have been), keep in mind that compiler follows `maximum munch`
rule, i.e. it reads the code sequentially and tries to make sense of
the longest possible legal sequence of characters. In your case these
are both `++i`s, but stringing them together does not make sense.

ans = ++i + +i ;
printf(" %d %d ",ans,i); // it prints 2 1
This is undefined behaviour. The `++i` bit can be executed at any time
during the evaluation of the expression (but before the sequence point
marked with `;` in your case, and after the previous one, which in your
case is the previous `;`). The same holds true for taking the value of
`i` for the second part of your expression (`+` in `+i` is unary plus,
which does exactly the same as +2 does in mathematics, i.e. nothing
much). Your compiler has chosen to first do `++i` then take value of
`i`, and evaluate expression as 2. It might have decided to do it the
other way around, and give you 0.

i=0;
ans = ++i + ++i ;
printf(" %d %d ",ans,i); // it prints 4 2
U.B. here as well, and your compiler chooses to be consistent and first
do both `++i`s, then perform addition, and give you 4 as a result.

i=0;
ans = ++i + ++i + ++i;
Same again here, but now the compiler apparently decides to deal with
the first two `++i`s first, add the results, only then perform the
third `++i`, and add that to the running total.

NB, all these are just my double-guessing what your compiler decided to
do on this particular occasion. Next week it may decide to do something
entirely different. Do not do these things (and do not rely on what
this compiler does when switching to a different one).
printf(" %d %d ",ans,i); // it prints 7 3
It is actually lucky that you get anything out of this program, as you
did not terminate the printf() with ''\n'' to force flushing of stdout.

You''ve missed these as well:

return 0;
}

How do 4 and 7 came can any one explain...




正如基思所指出的那样
的常见问题解答 http://www.c-faq.com/ (甚至可能是整个事情,不仅仅是3.2他建议的b $ b)会帮助你知道这些问题的答案(甚至

不要试图问他们立刻就是编译器了。


干杯


弗拉基米尔


-

你知道吗...


没有人读过这些东西吗?



As Keith pointed out elsethread reading the C FAQ at
http://www.c-faq.com/ (maybe even the whole thing, not just 3.2 he
suggested) would help you know the answers to such questions (or even
not to try to ask them of the compiler at all) immediately.

Cheers

Vladimir

--
Did you know ...

That no-one ever reads these things?


这篇关于增量操作如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆