该程序的答案是什么,我从turbo cpp和dev cpp得到了不同的答案 [英] What is the answer of the program , I got different answers from turbo cpp and dev cpp

查看:78
本文介绍了该程序的答案是什么,我从turbo cpp和dev cpp得到了不同的答案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  #include   <   iostream  >  
#include < conio.h >
使用 命名空间标准;
int main()
{
int R,S, N;
R = 45 ,n = 45 ;
S =(R--)+(++ R);
cout<< S;
return 0 ;
}





我的尝试:



程序的答案是什么,我得到了turbo cpp和dev cpp的不同答案

解决方案

他们都是对的;他们都错了!请参阅此处:为什么x = ++ x + x ++给我错误的答案? [ ^ ]


引用:

i从turbo cpp和dev cpp获得了不同的答案

这是一个暗示你不应该使用这样的代码。有关详细信息,请查看此处: c ++ - 未定义的行为和序列点 - 堆栈溢出 [ ^ ]。


引用:

该程序的答案是什么,我从turbo cpp和dev cpp获得了不同的答案



此程序没有正确答案,或者所有答案都是正确的。

这是一个灰色区域。

 S =(R--)+(++ R); 



编译器可以以任何合适的方式自由重写此代码,无论如何你阅读,理解,期待的方式。因此每个编译器都有自己的真实性。

因此对同一个变量进行多次递增/递减操作会使代码从编译器变为另一个不可预测。

这是其中的一个陷阱C语言。


#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
    int R,S,n;
    R=45,n=45;
    S=(R--) + (++R);
    cout<<S;
    return 0;
}



What I have tried:

what is the answer of the program, i got different answers from turbo cpp and dev cpp

解决方案

They are both right; they are both wrong! See here: Why does x = ++x + x++ give me the wrong answer?[^]


Quote:

i got different answers from turbo cpp and dev cpp

That is an hint you shouldn't use such a code. For details have a look here: c++ - Undefined behavior and sequence points - Stack Overflow[^].


Quote:

What is the answer of the program , I got different answers from turbo cpp and dev cpp


There is no correct answer to this program, or all answers are correct.
It is a gray zone.

S=(R--) + (++R);


The compiler is free to rewrite this code in any way it see fit, no matter the way you read, understand, expect it. So each compiler makes its own truth.
So multiple increment/decrement operations on same variable make the code unpredictable from a compiler to another.
This is 1 of the pitfalls of C language.


这篇关于该程序的答案是什么,我从turbo cpp和dev cpp得到了不同的答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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