325 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.

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

0 votes
0 votes
1 answer
1
stblue asked Oct 18, 2017
771 views
#include <stdio.h>int main(){ int a[] = {50, 60, 10, 30, 40, 20 }; int *b[] = {a+3, a+4, a, a+2, a+1, a+5 }; int c = b; c++; printf("%u, %u, %u\n", c-b, *...
1 votes
1 votes
2 answers
2
Sanyam Lakhanpal asked Jul 18, 2017
446 views
Please tell the output with explanation!#include<stdio.h>int main(){int a,b;printf("%d ",12);printf("%d ",143);printf("%d ",scanf("%d%d",&a,&b));}
0 votes
0 votes
0 answers
3
Dipayan asked Mar 16, 2022
222 views
With the required GATE rank, would the 4 year BS(Res) programme be considered equivalent to a BTech/BE degree? It is not explicitly mentioned on the IISc CSA website.