in Programming in C
724 views
1 vote
1 vote
find the output of following program

main()
{
    extern int a;
    a=5;
    printf("%d",a);
}
in Programming in C
724 views

4 Comments

error?

a is not declared.
1
1
Error >?
0
0
yes your ans is correct it is linker error

but why it is error

when I run the code on online turbo c compiler it print the value with no error

can you please explain why??
0
0
$a$ is declared as extern so its definition must be outside this function, but here no definition is present. And thus, when linker tries to link for the definition of $a$ outside of this function, and get linker error.
1
1

2 Answers

1 vote
1 vote
Best answer

it will give error.

because extern keyword is use for just for declaration of variable.

it simply means variable’s definition is somewhere in code.

that is why it give undefined reference error.

selected by
0 votes
0 votes
It will not give any compile time error but will give a runtime error (undefined symbol)!

4 Comments

haha, I never said gate questions will explicitly mention “turbo c” , however I said that whenever such controversial output questions arise, gate generally complies with what turbo c gives as o/p. Again I am not making up things on my own, I am only writing what I have learnt from my mentor and needless to say, he is considered to be the god of algo!
–1
–1
Yes ….during compilation extern will say compiler “Tension nehi lene ka apun haina”
0
0

@chokostar Good luck to you and your god mentor 👎 

0
0

Related questions