Вопрос

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.

Это было полезно?

Решение

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top