简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM