I have a few doubts regarding the following statement:
C allows a global variable to be declared again when first declaration doesn’t initialize the variable.
Consider the following snippet:
#include<stdio.h>
int x; //line 1
int x = 5; //line 2
int main(void)
{
printf("%d", x);
return 0;
}
OUTPUT: 5
Q1. In line 1, the first declaration of x does initialize the global variable x to 0 by default. Then why does the compiler not throw any error? If I rewrite line 1 and line 2 as:
int x=10;
int x=20;
The above snippet results in an error like redefinition of 'x'. Shouldn't I get the same error for the following also because x is, by default, initialized to 0?
int x;
int x=5;
Q2. Both the line 1 and line 2 are defining the global variable x. Is this statement correct? I have read that we can define a variable only once but we can declare it as many times as we want.