Consider this:
#include <stdio.h>
static int b;
int main()
{
{
int b;
{
extern int b;
b = 2;
}
}
printf("%d", b);
}
Here by definition the identifier 'b' which is assigned the value 2 must have be the one with external linkage. However for some reason the 'clang' C compiler doesn't complain and the output of this code is '2'.
I'm assuming this because of $6.2.2.4 point in the C standard:
For an identifier declared with the storage-class specifier extern in a scope in which a prior declaration of that identifier is visible,31) if the prior declaration specifies internal or external linkage, the linkage of the identifier at the later declaration is the same as the linkage specified at the prior declaration. If no prior declaration is visible, or if the prior declaration specifies no linkage, then the identifier has external linkage.
And as the previous visible declaration specifies no linkage ('int b').
Anyhow how can I make the declaration before the assignment to refer to the identifier with internal linkage (instead of the one with external such).
Edit: I see that this example is UB but this doesn't change my question.