加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 帮忙?为什么输出是这样的?

发布时间:2020-12-16 10:52:42 所属栏目:百科 来源:网络整理
导读:#include iostreamusing namespace std;int a = 8;int g(){ a++; return a - 1;}int f(){ a++; return a;}int main(){ cout g() " " f() " " g() + f() endl; system("PAUSE"); return 0;} Output is “11 11 18” 解决方法 函数的评估顺序在C中未指定.在代
#include <iostream>
using namespace std;

int a = 8;

int g()
{
    a++; 
    return a - 1;
}

int f()
{
    a++;
    return a;
}

int main()
{
    cout << g() << " " << f() << " " << g() + f() << endl;
    system("PAUSE");
    return 0;
}

Output is “11 11 18”

解决方法

函数的评估顺序在C中未指定.在代码中:

cout << g() << " " << f() << " " << g() + f() << endl;

编译器可以发出代码来调用f(),f(),g(),g()然后添加结果.或者它可以做其他事情.

这与使用cout,BTW没有什么关系 – 如果你编写这样的代码:

x = a() + b() * c();

无法保证调用a,b和c的顺序.这是全局变量为坏事的众多原因之一 – 您通常无法预测将如何调用更改它们的函数.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读