2 votes 2 votes #include <stdio.h> int main() { int a=10; extern int a; return 0; } Why this gives compilation error? Extern is just a declaration.So why it is giving error on compilation? Programming in C programming-in-c extern-variable + – rahul sharma 5 asked Nov 23, 2017 rahul sharma 5 1.1k views answer comment Share Follow See all 0 reply Please log in or register to add a comment.
0 votes 0 votes I think same variable name is used for simple variable declaration as well as special storage class declaration in the same class. Due to conflict it is giving an error. This error is showing on my gcc compiler. This is the reason behind it. Ashwin Kulkarni answered Nov 23, 2017 • edited Nov 23, 2017 by Ashwin Kulkarni Ashwin Kulkarni comment Share Follow See all 2 Comments See all 2 2 Comments reply rahul sharma 5 commented Nov 23, 2017 reply Follow Share But when i make int a=10 as a global variable then extern works.Whats the difference? 0 votes 0 votes Venkat Sai commented Jan 18, 2018 reply Follow Share when u declare a variable as extern int it means that this variable is defined some where else and asks the compiler not to allocate space for this variable hence here when u say int a=10; u are saying compiler to allocate space for this variable in the stack frame and when u use extern int a; u are telling the compiler this variable is a external variable called 'a' please dont allocate space for this dont u think ur words are conflicting ?? thats y u got compilation error when u moved int a =10 then it becomes truely an external variable and hence (by default every global variable is an external variable ) and hence works fine !!! 1 votes 1 votes Please log in or register to add a comment.