in Programming in C
323 views
4 votes
4 votes
CODE 1
#include <stdio.h>
int i;
int i=10;
int main(void) 
{

	return 0;
}

CODE 2
#include <stdio.h>
int i=0;
int i=10;
int main(void) 
{

    return 0;
}

Why Code 1 is not giving any compilation error  but code 2 does ?


I think in code 1 when first time i is declared ,at compile time it will be allocated memory and initialize to zero(implicitly) and in code 2 i just explicitly declare and define int i to be zero.

but i don't know how code 2 give error of redefinition.

in Programming in C
323 views

1 Answer

0 votes
0 votes

C allows a global variable to be declared again when first declaration doesn’t initialize the variable.

we get error: 

error: redefinition of ‘x’

Related questions