cpp得到了不同的答案

cpp得到了不同的答案

本文介绍了该程序的答案是什么,我从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的不同答案

解决方案


这是一个暗示你不应该使用这样的代码。有关详细信息,请查看此处: [ ]。



#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

解决方案


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[^].



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得到了不同的答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 10:53