An attempt to change the linkage of a name i
is made in this code. Is it legal in C/C++?
static int i = 2;
int i;
int main()
{
return 0;
}
In C++ your code is ill-formed (you have multiple definitions of variable i
) i.e a standard conformant compiler is required to issue an error message
$3.2.1 (C++03)
No translation unit shall contain more than one definition of any variable, function, class type, enumeration type or template.
In C99 your code invokes Undefined Behaviour because 6.2.2/7 says
If, within a translation unit, the same identifier appears with both internal and external linkage, the behavior is undefined.
No. In C I get this error:
test.c:2: error: non-static declaration of ‘i’ follows static declaration
test.c:1: note: previous definition of ‘i’ was here
In C++, these:
test.cpp:2: error: redefinition of ‘int i’
test.cpp:1: error: ‘int i’ previously defined here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With