C++ Quiz160 of 173
Difficulty:
⬅️ Prev: 333Quiz ID: 334Next: 335 ➡️

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

#include <iostream>

int f()
{
    std::cout << 1;
}

int main()
{
    f();
    std::cout << 2;
}
Hint

f() is missing a return statement, but this is not a compilation error.

Reveal answer

The program is (or may be) undefined

Explanation

This is not a compilation error, but undefined behaviour. According to §[stmt.return]¶4 in the standard:

Flowing off the end of a constructor, a destructor, or a non-coroutine function with a cv void return type is equivalent to a return with no operand. Otherwise, flowing off the end of a function that is neither main (§[basic.start.main]) nor a coroutine (§[dcl.fct.def.coroutine]) results in undefined behavior.

⬅️ Prev: 333Quiz ID: 334Next: 335 ➡️