出把这个计划.Confused的? [英] Out put of this program .Confused ?

查看:112
本文介绍了出把这个计划.Confused的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我坚持了这个问题。

INT I = 5,;
A = ++ I + I + + + +我+ I ++ - --i;
的printf(%d个,一);

据我'一个'应该是20。
一个= 6 + 6 + 8-8
然而,在执行我找到了答案是18,我做错了什么?一步一步的解释将是有益的。


解决方案

我在我的博客文章写这一个详细的方式在:的 http://blog.susam.in/2010/05/sequence-points.html

我张贴一些摘录在这里。

问题的一个特殊类型在C编程论坛时间后问时间。有关于刺激在论坛上有经验的程序员这样的问题两件事情。首先,这种类型的问题是如此普遍,许多甚至不希望他们做出回应,即使这意味着发布一个链接到一个类似的问题已经回答了另一个线程。第二,更重要的是,即使有人试图提供正确答案的问题,还有很多其他人谁忽视它填补螺纹不正确的答案。

该问题通常涉及寻找code的输出是这样的。

 的#include<&stdio.h中GT;诠释的main()
{
    INT I = 5;
    的printf(%d个%D \\ n,我,我 - ,+ +我);
    返回0;
}

输出为5 6 5时,使用GCC编译和6 6 6时,与Microsoft C / C ++编译器来使用Microsoft Visual Studio编译。

这样的C程序的行为是不确定的。在声明的printf(%d个%D \\ n,我,我 - ,+ +我);和+ = A + + + +一个分号;,是唯一的序列点。 Ç保证一个给定的前pression的所有的副作用是通过在节目的下一个序列点完成。如果下一个序列点之前与相互影响的副作用两个或两个以上的操作发生的行为是不确定的。当不同的编译器编译此类code行为可能不同。

在我引用来自ISO / IEC标准的有关章节,让我从K&放东西报价; R。在第2.12节的书(precedence和计算顺序),作者写道,


  C,最喜欢的语言,不指定顺序的


  
  

操作者的操作数进行评估。 (唯一的例外是与放大器;&放;, ||,?,
  和','。)例如,像

声明

  X = F()+ G();f可以g或反之亦然之前评估;因此,如果任一F或G型


  
  

改变在其上的其他依赖的变量,x可以取决于
  为了评价。中间结果可以存储在临时
  变量,以确保特定序列


他们在本节提供了一个例子。


 一个不愉快的局面是由代表发言一个由[i] =我++;问题是,标是否我还是旧值


  
  

新。编译器可以以不同的方式相互$ P $角本,并生成
  不同的答案取决于他们之间的pretation。


如果您想了解更多关于这个,下载ISO / IEC 9899 C标准,把438页的附录C - 序列点。它列出了所有的序列点。 ;是其中之一。 +和++运算符不序列点。

接下来,读出部分5.1.2.3(程序执行),2点。


 访问volatile对象,修改对象,修改


  
  

文件,或调用任何做这些操作都是一个函数
  副作用,11),这是在执行状态的变化
  环境。一个前pression评估可能产生的副作用。在
  执行顺序称为序列中的某些特定点
  点,previous评估所有的副作用应齐全
  后续评估的无副作用不得发生。 (一个
  序列点总结在附录C中给出。)


So I am stuck with this question.

int i=5,a;
a=++i + i++ + ++i + i++ - --i;
printf("%d",a);

According to me 'a' should be 20. a=6+6+8-8 However, on execution I found the answer to be 18. What I am doing wrong? A step by step explanation would be helpful.

解决方案

I have written about this in a detailed manner in my blog post at: http://blog.susam.in/2010/05/sequence-points.html

I am posting some extracts from it here.

A particular type of question is asked time after time in C programming forums. There are two things about such questions that irritate the experienced programmers in the forums. Firstly, this type of questions is so common that many don't even want to respond to them even if it means posting a link to another thread where a similar question has been answered. Secondly, and more importantly, even if someone tries to provide the correct answer to the question, there are many others who ignore it and fill up the thread with incorrect answers.

The questions usually involve finding the output of a code like this.

#include <stdio.h>

int main()
{
    int i = 5;
    printf("%d %d %d\n", i, i--, ++i);
    return 0;
}

The output is 5 6 5 when compiled with gcc and 6 6 6 when compiled with Microsoft C/C++ compiler that comes with Microsoft Visual Studio.

The behaviour of such C programs is undefined. In the statements printf("%d %d %d\n", i, i--, ++i); and a += a++ + a++;, semicolon is the only sequence point. C guarantees that all side effects of a given expression is completed by the next sequence point in the program. If two or more operations with side effects affecting each other occur before the next sequence point, the behavior is undefined. Such code may behave differently when compiled with different compilers.

Before I quote the relevant sections from the ISO/IEC standard, let me quote something from K&R. In Section 2.12 (Precedence and Order of Evaluation) of the book, the authors write,

C, like most languages, does not specify the order in which the

operands of an operator are evaluated. (The exceptions are &&, ||, ?:, and ','.) For example, in a statement like

x = f() + g();

f may be evaluated before g or vice versa; thus if either f or g

alters a variable on which the other depends, x can depend on the order of evaluation. Intermediate results can be stored in temporary variables to ensure a particular sequence.

They provide one more example in this section.

One unhappy situation is typified by the statement

a[i] = i++;

The question is whether the subscript is the old value of i or the

new. Compilers can interpret this in different ways, and generate different answers depending on their interpretation.

If you want to read more on this, download the ISO/IEC 9899 C standard and turn to page 438 for Annex C – Sequence Points. It lists down all the sequence points. ; is one of them. + and ++ operators are not sequence points.

Next, read section 5.1.2.3 (Program execution), point 2.

Accessing a volatile object, modifying an object, modifying a

file, or calling a function that does any of those operations are all side effects,11) which are changes in the state of the execution environment. Evaluation of an expression may produce side effects. At certain specified points in the execution sequence called sequence points, all side effects of previous evaluations shall be complete and no side effects of subsequent evaluations shall have taken place. (A summary of the sequence points is given in annex C.)

这篇关于出把这个计划.Confused的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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