Pergunta

int x; //line 1
int x; //line 2
int main()
{
    return 0;
}

Both line 1 and line 2 declare as well as define the variable x. But the code doesn't give any linker errors. Why is it so?

Compilation fails only when I initialize x in both the lines.

int x=3;
int x=3;
int main(){
return 0;
}

I thought compilation would fail in code 1 because of multiple definition of variable x.

I'm new to programming, so please pardon any mistakes.

Foi útil?

Solução

It works because the definitions in my code are tentative in nature. So they are initially taken as declarations and upon failing to find any other instance of definition, they are combined into a single definition.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top