banner
cells

cells

为美好的世界献上 code

Q106

Q106#

#include <iostream>

extern "C" int x;
extern "C" { int y; }

int main() {
    std::cout << x << y;
}

Answer#

click to see answer The program is undefined

Explain#

A declaration directly contained in a linkage-specification is treated as if it contains the extern specifier for the purpose of determining the linkage of the declared name and whether it is a definition.
extern "C" int x; //is just a declaration
extern "C" { int y; } //is a definition

"Every program shall contain exactly one definition of every non-inline function or variable that is odr-used in that program outside of a discarded statement; no diagnostic required."

The result: x is never defined but it is optional for the compiler to print an error. The behaviour of this program is undefined.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.