in Programming retagged by
352 views
1 vote
1 vote

#include <stdio.h>

int i =20;

int main()

{

    int i = 10;

    extern int i;

    printf("%d",i);

 

}

Why does this program gives Compiler error??

please explain

in Programming retagged by
352 views

2 Answers

3 votes
3 votes
Best answer

Yes this program should give compiler error

because 

selected by

4 Comments

Are you saying that if we remove printf call then there will be no error?
0
0

@Arjun sir what i wanted to say is 

there is a conflict between the two variables of same names

it is same as defining two variables with same names like 


#include <stdio.h>

int main()
{
    int b = 20;
    int b = 30;
    return 0;
}


which results in compiler error

1
1
:) That’s not what you have written.

And is the issue with defining a variable multiple times or declaring a variable multiple times?
0
0

@Arjun ok sir i will change the answer and will write in detail 😅

i think the issue is with defining of a variable multiple times 

my reasoning is if we declare a variable we are just saying to the compiler that there is a so and so variable ( we can use extern int i many times without any issue )

but while defining we need to allocate memory this is where things go crazy ( compiler will be like :  i already allocated memory for this dude why am i seeing him again in the line 😋 )

0
0
0 votes
0 votes
Before the Int Main() datatype and variable declared

Related questions