简体   繁体   中英

Does the output depend on the compiler?

I have a code. The code prints 1236 (g++ 7.5.0)

Does the output depend on the compiler? (eg output can be 3216 )

#include <bits/stdc++.h>


using namespace std;


int foo(int& x) {
    std::cout << ++x; 
    return x;
}


int main() {
    int i = 0;
    cout << foo(i) + foo(i) + foo(i) << endl; // 1236
}

No, the output does not depend on the compiler (modulo the bits/stdc++.h nonsense). The order in which the three calls foo(i) are evaluated is unspecified, but that doesn't affect the output: function calls are not interleaved, so some call will increment i to 1, print that, and return it (as a copy ), then one of the other two will assign, print, and return 2, then the last will do 3, and their return values will always sum to 6.

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