简体   繁体   English

为什么 cout 和 return 在以下 C++ 代码中给出不同的值?

[英]Why does cout and return give different values in the following c++ code?

#include <iostream>

using namespace std;

int fn1 (){
    int a = 5;
    int b = 6;
    cout << (++a > b--)? (a+b):(a-b) ;
    a = 5;
    b = 6;
    return (++a > b--)? (a+b):(a-b);
}

int main (){
    cout << fn1();
}

// Output: 01 // 输出:01

Operator precedence.运算符优先级。

cout << (++a > b--)? (a+b):(a-b);

is parsed as:被解析为:

(cout << (++a > b--))? (a+b):(a-b);

which first evaluates the cout << then the rest.首先评估cout <<然后是其余的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C和C ++编译器为以下代码提供不同的消息,为什么? - C and C++ compiler give different messages for the following code, why? 为什么此C ++代码在不同的编译器上给出不同的输出? - Why does this C++ code give different output on different compilers? 为什么不同的C ++编译器对此代码给出不同的结果? - Why do different C++ compilers give different results for this code? 为什么C ++ stdlib rand()函数为跨平台的相同种子提供不同的值? - Why does the C++ stdlib rand() function give different values for the same seed across platforms? 为什么我的 c++ 斐波那契序列代码在 46 次迭代后给出负值 - Why does my c++ fibonacci sequence code give negative values after 46 iterations C ++-为什么[pointer]和this-&gt; [pointer]给出不同的值? - C++ - Why do [pointer] and this->[pointer] give different values? 为什么cout &lt;&lt;&r与cout &lt;&lt;(void *)&r给出不同的输出? - Why does cout << &r give different output than cout << (void*)&r? 为什么下面的代码给我 0 作为输出 - Why does the following code give me 0 as the output 为什么我的代码通过在 c++ 中注释单个打印 cout 语句来给出不同的 output? - Why my code is giving different output by just commenting a single printing cout statement in c++? 为什么在C ++和Java中此float操作会产生不同的结果? - Why does this float operation in C++ and Java give different results?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM