C++ Quiz50 of 173
Difficulty:
⬅️ Prev: 120Quiz ID: 121Next: 122 ➡️

According to the C++23 standard, what is the output of this program?

#include <iostream>

int main() {
  int a = 10;
  int b = 20;
  int x;
  x = (a, b);
  std::cout << x;
}
Hint

What effect does parentheses have on operator precedence? What is the value of an expression involving the comma operator?

Reveal answer

20

Explanation

The comma operator is applied on two expressions: a and b.

According to §[expr.comma]¶1 in the standard:

A pair of expressions separated by a comma is evaluated left-to-right; the left expression is a discarded-value expression. (...) The type and value of the result are the type and value of the right operand (...).

The right operand here being b, with the value 20. This is then the resulting value of the expression (a, b), and 20 is assigned to x.

⬅️ Prev: 120Quiz ID: 121Next: 122 ➡️