Different results in different compilers C++

Shervan360 1,661 Reputation points
2020-10-07T16:22:31.407+00:00

Hello,

I have different results in different compilers. Is this undefined behavior?

#include <iostream>   
using namespace std;  
  
  
int main()  
{  
	int x = 4;  
  
	cout << (x == 0) << (x = 0);  
  
	return 0;  
}  

30688-screenshot-2020-10-07-121729.jpg

30762-screenshot-2020-10-07-122110.jpg
30689-screenshot-2020-10-07-120907.jpg

Developer technologies C++
{count} votes

4 answers

Sort by: Most helpful
  1. Viorel 122.5K Reputation points
    2020-10-07T17:34:30.21+00:00

    Note that a similar sample — ‘cout << i << i++’ — is declared “undefined behaviour until C++17”: https://en.cppreference.com/w/cpp/language/eval_order.

    In Visual Studio 2019 you can try different values of “C++ Language Standard” option in Project Properties. Make sure that you affect and run the Release configuration.

    In case of C++17 your code displays “00”. In case of C++14 it displays “10”.

    1 person found this answer helpful.
    0 comments No comments

  2. WayneAKing 4,931 Reputation points
    2020-10-07T17:40:01.7+00:00

    In general, the order of evaluation of expressions in a statement is unspecified.

    So the compiler may evaluate expressions in a statement from left to right
    or right to left. The expressions in the statement will be executed according
    to the rules of associativity and precedence.

    https://en.cppreference.com/w/cpp/language/eval_order

    So in the statement

    cout << (x == 0) << (x = 0);

    the output will show the result of the expression (x == 0) before it shows
    the result of the expression (x = 0). But the two expressions may be evaluated
    in either order before the output is performed.

    Note that changes in the C++ Standard from version to version may determine
    whether or not a statement exhibits "undefined behavior".

    • Wayne
    1 person found this answer helpful.
    0 comments No comments

  3. MMS 05 61 Reputation points
    2020-10-08T10:31:20.743+00:00

    Different Compilers Will Always Different Behaviours for Some Code. That's not an Unusual Behaviour, You Can Probably Use the Microsoft Visual C++ Compiler, Its Way Better and Trusted than Other Compilers

    1 person found this answer helpful.
    0 comments No comments

  4. Igor Tandetnik 1,116 Reputation points
    2020-10-08T14:20:33.747+00:00

    This program is well-defined under C++17 or later standard; and exhibits undefined behavior under earlier C++ standard versions.

    C++17 introduced two new rules that make it well-defined. First, evaluations of operands of a function call are indeterminately sequenced with respect to each other (as opposed to unsequenced in prior versions). So something like f((x == 0), (x = 0)) was undefined before C++17, and is merely unspecified after.

    Second, for certain operators, including shift operators, the order of evaluation of their operands is guaranteed. This is true even when those operators are overloaded and result in a function call; in which case the order of evaluation of its arguments is consistent with that of a corresponding built-in operator.

    In short, starting with C++17, the program is equivalent to

    std::cout << (x == 0);
    std::cout << (x = 0);
    

    and should reliably print 00. Under prior versions, it exhibits undefined behavior and can print anything at all.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.