Home > Software design >  Does the output depend on the compiler?
Does the output depend on the compiler?

Time:01-29

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

Does the output depend on the compiler? (e.g. 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
}

CodePudding user response:

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.

  • Related