Understanding “extern” keyword in C

I’m sure that this post will be as interesting and informative to C virgins (i.e. beginners) as it will be to those who are well versed in C. So let me start with saying that extern keyword applies to C variables (data objects) and C functions. Basically extern keyword extends the visibility of the C variables and C functions. Probably that’s is the reason why it was named as extern.

Though (almost) everyone knows the meaning of declaration and definition of a variable/function yet for the sake of completeness of this post, I would like to clarify them. Declaration of a variable/function simply declares that the variable/function exists somewhere in the program but the memory is not allocated for them. But the declaration of a variable/function serves an important role. And that is the type of the variable/function. Therefore, when a variable is declared, the program knows the data type of that variable. In case of function declaration, the program knows what are the arguments to that functions, their data types, the order of arguments and the return type of the function. So that’s all about declaration. Coming to the definition, when we define a variable/function, apart from the role of declaration, it also allocates memory for that variable/function. Therefore, we can think of definition as a super set of declaration. (or declaration as a subset of definition). From this explanation, it should be obvious that a variable/function can be declared any number of times but it can be defined only once. (Remember the basic principle that you can’t have two locations of the same variable/function). So that’s all about declaration and definition.

Now coming back to our main objective: Understanding “extern” keyword in C. I’ve explained the role of declaration/definition because it’s mandatory to understand them to understand the “extern” keyword. Let us first take the easy case. Use of extern with C functions. By default, the declaration and definition of a C function have “extern” prepended with them. It means even though we don’t use extern with the declaration/definition of C functions, it is present there. For example, when we write.

    int foo(int arg1, char arg2);

There’s an extern present in the beginning which is hidden and the compiler treats it as below.

    extern int foo(int arg1, char arg2);

Same is the case with the definition of a C function (Definition of a C function means writing the body of the function). Therefore whenever we define a C function, an extern is present there in the beginning of the function definition. Since the declaration can be done any number of times and definition can be done only once, we can notice that declaration of a function can be added in several C/H files or in a single C/H file several times. But we notice the actual definition of the function only once (i.e. in one file only). And as the extern extends the visibility to the whole program, the functions can be used (called) anywhere in any of the files of the whole program provided the declaration of the function is known. (By knowing the declaration of the function, C compiler knows that the definition of the function exists and it goes ahead to compile the program). So that’s all about extern with C functions.

Now let us the take the second and final case i.e. use of extern with C variables. I feel that it more interesting and information than the previous case where extern is present by default with C functions. So let me ask the question, how would you declare a C variable without defining it? Many of you would see it trivial but it’s important question to understand extern with C variables. The answer goes as follows.

    extern int var;

Here, an integer type variable called var has been declared (remember no definition i.e. no memory allocation for var so far). And we can do this declaration as many times as needed. (remember that declaration can be done any number of times) So far so good. :)

Now how would you define a variable. Now I agree that it is the most trivial question in programming and the answer is as follows.

    int var;

Here, an integer type variable called var has been declared as well as defined. (remember that definition is the super set of declaration). Here the memory for var is also allocated. Now here comes the surprise, when we declared/defined a C function, we saw that an extern was present by default. While defining a function, we can prepend it with extern without any issues. But it is not the case with C variables. If we put the presence of extern in variable as default then the memory for them will not be allocated ever, they will be declared only. Therefore, we put extern explicitly for C variables when we want to declare them without defining them. Also, as the extern extends the visibility to the whole program, by externing a variable we can use the variables anywhere in the program provided we know the declaration of them and the variable is defined somewhere.

Now let us try to understand extern with examples.

Example 1:

int var;
int main(void)
{
   var = 10;
   return 0;
}

Analysis: This program is compiled successfully. Here var is defined (and declared implicitly) globally.

Example 2:

extern int var;
int main(void)
{
  return 0;
}

Analysis: This program is compiled successfully. Here var is declared only. Notice var is never used so no problems.

Example 3:

extern int var;
int main(void)
{
 var = 10;
 return 0;
}

Analysis: This program throws error in compilation. Because var is declared but not defined anywhere. Essentially, the var isn’t allocated any memory. And the program is trying to change the value to 10 of a variable that doesn’t exist at all.

Example 4:

#include "somefile.h"
extern int var;
int main(void)
{
 var = 10;
 return 0;
}

Analysis: Supposing that somefile.h has the definition of var. This program will be compiled successfully.

Example 5:

extern int var = 0;
int main(void)
{
 var = 10;
 return 0;
}

Analysis: Guess this program will work? Well, here comes another surprise from C standards. They say that..if a variable is only declared and an initializer is also provided with that declaration, then the memory for that variable will be allocated i.e. that variable will be considered as defined. Therefore, as per the C standard, this program will compile successfully and work.

So that was a preliminary look at “extern” keyword in C.

I’m sure that you want to have some take away from the reading of this post. And I would not disappoint you. :)
In short, we can say

1. Declaration can be done any number of times but definition only once.
2. “extern” keyword is used to extend the visibility of variables/functions().
3. Since functions are visible through out the program by default. The use of extern is not needed in function declaration/definition. Its use is redundant.
4. When extern is used with a variable, it’s only declared not defined.
5. As an exception, when an extern variable is declared with initialization, it is taken as definition of the variable as well.





  • SquareBunny

    …though excellent none the less

  • SquareBunny

    It might be just the pot talking, but this is way too long…

  • arindam mandal

    extremely helpful…

  • Shashank Srivastav

    Lovely article. I believe you covered everything and all of it was thoroughly explained. Learned a lot. Especially that extern is default for functions.

  • Rose Robinson

    Thanks for a great article! You described everything very thoroughly and I felt like I’d learned a lot after reading it!

  • Mukul Taneja

    int var; // its a defination not a declaration right?
    Means you want to say that in general we dont declare variables..we always define variables and declaration and defination done in function case.

  • Vishnu

    Great

  • Ambadas Hibare

    Thank you

  • Guest

    gcc compiler in ubuntu gave on error on this

    #include
    int main()
    {
    extern int a =5;
    a=4;
    printf(“%d”,a);
    return 0;
    }

    can someone explain me why while
    declaring extern int a globaly produces a warning but compiles

    • Galon

      External variable must be defined outside any function block to be a global variable (doesn’t matter if it is static or not). In the example it is defined as a local to function ‘main’. It is not a really global variable as it couldn’t accessed by other function directly.

  • Sudarshan Kj

    very descriptive explanation .
    thanks!

  • Olof

    Thank you.

  • pulpfree

    appreciate it!!

  • Runcy

    Excellent stuff!

  • Keshav

    By far the best explanation I have read.

  • Aadi

    very well explained. Thanks very much!

  • Shlomo

    just want to point out that a linkage error will occur and not a compilation error as written above.

  • iamkris

    how do i use this in keyword analysis, can you give a sample program? I want to create a program that responds to the questions i ask, there will be possible structure of the question but one possible answer. TY

  • jugal

    ex-5 gives following error if extern int var = 0; line written inside main function.
    error : ‘var’ has both ‘extern’ and initializer

    int main(void)
    {
    extern int var = 0;
    var = 10;
    return 0;
    }

    any one explain why it is happened??

    • Zaigham

      Which compiler you are using?

    • Mallesh Koujalagi

      since extern int var = 0; declaration and initialized with { }, which mean not global variable, it act as local variable and if we use extern to local then will definately give error.

  • Prateek

    void main(){
    extern int i; //as per u its only declaration
    int i;// as per u its declaration + definition

    i think that is y i’m getting the error multiple declarations for i
    how to write code pls help!!
    if i’m typing ur example 5, then i’m getting an error, extern variables cannot be initialised…
    then how to use extern??

  • sagar

    clearly explained !

  • Satya

    good one

  • xinning wang

    great summary.

  • av

    nice!

  • gt

    #include
    extern int a;

    int main()

    {

    extern int a;

    int a=34;

    printf(“%d”,a);

    return 0;

    }

    why this code is showing error??

    • RJ

      this code shud not give error..may be there is some other mistake..please check it..

    • neha

      extern variable declaration is a promise to the compiler that there would be a definition of a globalvariable some place else. Local variables do not qualify as fulfillments of the promise to the compiler, because they are invisible to linkers. In a sense, extern declarations are similar to forward declarations of functions: you say to the compiler “I know this function is there, so let me use it now, and let linker take care of locating the actual implementation”.

    • dattu

      this type of definition showing error ( linker error).why it is showing as error?

    • venky

      its like neha said, inker looks for that variable definition outside the scope of where it is defined.(depends also on where its defined like ex-5, it is declared & defined in global scope. If you do the same thing inside main it’ll be throw error, instead of warning.

      try this code for better understanding.

      #include
      int a;
      int main()
      {
      extern int a; //looks for a outside of its scope(main function here)
      int a=34;
      printf(“%d”,a);
      return 0;
      }
      The above code works….
      or
      “test.c” has definition of ‘a’ inside

      #include
      extern int a;
      int main()
      {
      extern int a; //looks for a outside of its scope(main function here)
      int a=34;
      printf(“%d”,a);
      return 0;
      }
      If you link the test.c while running the above code, it works…

      BASICALLY
      1)extern for variable looks for its definition outside its scope.
      2)If above condition fails it allows some wild card entries with warnings.

  • codeit

    is this right?
    extern int var;

    int main(void)

    {

    int var = 10;

    return 0;

    }

    • RJ

      yes

      • jyoti

        no

        • Mallesh

          It’s right, until and unless var global variable defined (memory location)

          • Gaurav

            Even if we define the extern var. It would not be a problem as the two ‘var’ have different scope. local var would take priority unless scope is explicitly specified.

  • Ponni

    Awesome :)

  • vidisha bhatnagar

    thankyu..ur explanation is very gudd

  • Sachin

    awesome :)

  • ajay

    awesome!!! thanx a lot

  • tarun

    thanks a lot dude

  • Sambit

    Thanks a lot, it was very helpful.

  • jinzhi chen

    wow, good explanation, thanks!

  • gautam chawla

    very nice explaination…keep it up :)

  • Manohar Singh Aswal

    nice explanation with example to understand all about using extern keyword.

  • hao

    Thanks, this helps a lot :)

  • xiyal

    Nice, thanks :)

  • Saadhana

    really clear explanation.. Thank you…

  • Neda

    Thanks, very concise and helpful:)

  • Dithya

    Thanks :)

  • Yasmin Mohamed

    so useful … thanks :)

  • kkk

    where it is used?

  • srikanth

    very good post. Helps me a lot to understand.

  • jhabar choudhary

    nice post

  • Urvish

    great…..

  • Pri

    great post !!! Thank you so much.

  • parashar

    good insight !

  • a.n.saqibdar

    Nice one… Thanks

  • sandeep

    vry nyc article :) (y)

  • guest

    are we extern it for other data types …i mean defined in one data type say int and declare in other data type say char..?

  • Guest

    what does it mean by ..we can declare a variable any num of times means u can have like int a; and char a; without error????????

    • Arpit Jain

      No. It means you can write `extern int a` line any number of times in your program.

      • RJ

        in tat case i can write a=10 many time times..

  • Durgesh

    PS: thats why Jay is not getting error

  • Durgesh

    my dear friend int var ; is not a definition its an tentative definition . and all tentative definitions are combined to actual ones when the compiler finds the corresponding real definition and if it does not it initializes it to default value.
    suppose you have a definition of variable in file1.c

    int p=67;

    in file2.c

    you defined:
    int p;
    int main(){

    printf(“%d”,p);
    }

    what would be the answer? no it is not the error redefinition of p:P

  • PRATIK

    include

    #include

    void increment()

    {

    extern int i=1;

    i=i+1;

    printf(“%dn”,i);

    }

    main()

    {

    increment();

    increment();

    }

  • Pixel

    Nice Post. Thanks

  • B.Rizzle

    Thanks for this post. You are the man.

  • jay

    #include
    int var;
    int var;
    int var;

    int main(void) {
    int var = 564 ;
    printf(“%d n”,var); return 0;
    }

    why it is not giving any error as var is defined and declared three times?

    • Pixel

      Because “var” which is int type variable is declare three time but not defined there is no memory allocated for var.. second paragraph last third line.

      “variable/function can be declared any number of times but it can be defined only once. (Remember the basic principle that you can’t have two locations of the same variable/function)”

      • jay

        int var;
        the var is declared as well as defined as mentioned above

        “Now how would you define a variable. Now I agree that it is the most
        trivial question in programming and the answer is as follows.

        int var;

        Here, an integer type variable called var has been declared as well as defined”
        but you are telling that it is only declared….?
        because if we want only declaration it should use extern keyword
        ……as
        extern int var;
        it is only declaration……

        • Pixel

          Its my mistake I perceived int var is written inside the main function..
          Yes, int var is a globle variable which is declared and define at the same time..

          But as per my understanding a variable can be re define any while any time inside the program if it is globle.

          • jay

            okkkk its the only thought we can give on it………….

            ok then plz give a thought to this one also……….

            #include

            int main(void)
            {
            int foo();
            int foo();
            int foo(){}

            return 0;
            }
            why it is giving an error as foo() is declared two times and third time it is declared as well as defined…….as extern is added as by default……but if it is outside main() it is working well…….

          • Pixel

            Hello Jay,

            There are two things I want to comment on. First is function should be declared before it getting used and Second one which is applicable in you case is a function definition can not be written inside another function.

            I am seeing why this program is throwing ” static declaration of foo follows non-static declaration” but as per me this error reporting depends on GCC versions.

          • jay

            #include
            //int foo();
            int main(void)
            {
            int foo(){}

            }

            as it is running without error ….so a function can be dfined in another function as contrary to your ssecond comment i.e. ” Second one which is applicable in you case is a function definition can not be written inside another function.

          • Pixel

            yes jay, A function implementation can be written inside function but it is nothing as just like an local (auto) function, which can be partially said as “Not a moulder approach even though it is implemented through function”.
            I have never used this type of coding anywhere.

          • Guest

            I don’t think so c-standard mentions any thing about nested functions. I guess it’s just a prank with a compiler.

          • Arpit Jain

            If you like your function definition then have to look on this program.

            #include
            void go(void (*foo)()){
            foo(); // to access foo from outside you need to use function pointers. So its better to avoid them.
            }
            int main(void) {
            void foo(){
            printf(“From in”);
            }
            foo();
            go(foo);
            return 0;
            }

            In my opinion you should avoid function pointers

      • Mohit

        Why does it gives error?
        #include
        int main(void) {
        int var;
        int var;
        var = 564 ;
        printf(“%d n”,var); return 0;
        }

    • kkk

      its showing error buddy

  • Sandhya Sasidharan

    nice post thanks :)

  • GUNJAN

    thanks man great work there really helped a lot

     
    /* Paste your code here (You may delete these lines if not writing code) */
     
    • abhishek paweria

      hii i would presume as if the variable is static then the compiler won’t let you use it outside its scope i.e outside its block , so you cannot allocate it memory space
      but if the variable is global then surely it can be used in any scope in the program and it should be because its global
      thats why compiler permits it

  • Shubham

    TO use variable which is declared outside main in one file in second file,we have to include first file in second file.So what is the need for declaring that variable in that second file …
    more appropriately what is the need of extern keyword

    • garvit sharma

      Basics:
      ——-
      While developing a big software all the functions can not be written in a single [.c] file because doing so will decrease the readability of the software. Also it would become very difficult to understand, reuse and maintain the software code. So to avoid these situations a software is being divided into modules and a module is nothing but the [.c] file which consists of certain functions (subroutines) and global variables.

      Extern in picture:
      ——————
      Now different modules uses global variables which gets modified by different functions in different modules. To achieve this facility we need to declare those variables as “extern”.

      Conc:
      —-
      If you are curious to know how that works then you should write different modules and manipulate those variables.

  • Gagandeep

    Greatly explained !

     
    /* Paste your code here (You may delete these lines if not writing code) */
     
  • garvit

    Great!! very well teaches the concept of extern.

  • ashatm

    if you try to re declare a local variable it shows an error but an extern variable can be re declared any number of times..

  • ashatm

    isn’t it wrong that u can declare a function/variable any number of times you want like said by the writer?
    I guess we can declare a variable only once and can overwrite the value stored in it as many times as we like..

  • shek8034

    Sorry, but i don’t understand this line :(Second para, last lines)
    “It should be obvious that a variable/function can be declared any number of times but it can be defined only once.”

    I think its wrong, because compiler gives error if we try to re-declare a function or a variable…..
    Correct me if i m wrong ??

    • Omsai Jadhav

      //here is the code i have declared variable x 3 times and
      //defined only once..and its working fine.
      //you can check it out with functions also it must work
      #include
      extern int x;
      extern int x;
      int main()
      {
      int x=10;
      printf(“%d”,x);
      return 0;
      }
      extern int x;

      • shek8034

        ok. Thats all about variables. What about functions ?
        Can we declare function more than once in same file ?

    • Anchika

      This line is Prefectly right.. because Rule is “You cannot define more than once but declare just once.”

  • rohit

    Just Fabulous nice work…!!

  • Harish Verma

    Nice post!!

  • Adarsh ron

    extern int i=0 will not throw error bcoz it is defined with its default initialization value of 0(gets memory in initialized data segment).But extern int i=1 will throw error bcoz it is trying to redefine the default value at declaration time.Note extern int =0 is a special case.

    • Sanjay Agarwal

      Hi Asarsh..I just tested my code which includes “extern int i = 1″. But it is not giving any compilation error.

  • John A.

    extremely useful, and as you said not just to newbies but also to experts. kudos.

  • Sourav

    if i write

     
    #include<stdio.h>
    
    int main(void)
    {
    	  extern int var=564 ;
     printf("%d",var);
     return 0;
    }
     

    then it is showing error like
    Error NONAME00.CPP 5: extern variable cannot be initialized in function main()
    Error NONAME00.CPP 6: Undefined symbol ‘var’ in function main()
    but when i write

     
    #include<stdio.h>
     extern int var=564 ;
    int main(void)
    {
    	 
     printf("%d",var);
     return 0;
    }
     

    its showing o/p as 564 correctly….can u plz explain

    • sreekanth

      It would be better if you figure out the problem yourself based on some hints that I shall provide.
      Please analyze the memory layout for the program in terms of storage locations of the variables :- i.e, Uninitialized Data Segment (bss),Initialized data segment and Stack. Further,try to find an explanation yourself based on this storage concept VS the Scope and Lifetime of variables during the program execution.
      Still,if you are unclear ,please ask.

      Sk

       
      /* Paste your code here (You may delete these lines if not writing code) */
       
    • John A.

      The 1st code is not valid because the C language stipulates that:” Identifiers declared within a function with the storage class extern must have an
      external definition somewhere
      outside the function in which they are declared.” C Reference Manual 5th Ed. (page 70); and so you can see that in your case(1st code) the extern variable is declared and defined in main function,… hope you get that.

       
      /* Paste your code here (You may delete these lines if not writing code) */
       
    • John A.

      To complement my previous comment, if you want the 1st code to compile successfully add: int var outside (before) main, so that the code now looks as below:

       
      #include<stdio.h>
      
      /* This defines the variable outside main function*/
      int var; 
      /*************************************************/
      
      int main(void)
      {
      	extern int var = 564 ;
      	printf("%d \n",var);
      	return 0;
      }
      
       
      • pawankalyan.lovely@gmail.com

        what an ans lol…do u know c…

         
        /* Paste your code here (You may dgelete these lines if not writing code) */
         
        • sweetgirl

          john is right..and pawan do u know c??

        • John A.

          Funny; there’s no need for you to just make noise, show your code. I think from your code I can tell whether I “know C” or not.

           
          /* Paste your code here (You may delete these lines if not writing code) */
           
    • http://www.noroadsleft.com/ Amit Dhiman

      Because , the variable you are declaring in main() getting its memory allocation on stack it does’t have scope outside main() . and the variable you are declaring outside main() is in global scope . the initialization of extern variable should be in global scope .

      • Rakesh

        very well said Amit

  • anonymous

    Thanks

     
    /* Paste your code here (You may delete these lines if not writing code) */
     
  • Pushpi

    Really very nicely explained… :)

    Thanks :)

  • sourav

    mind blowing explanation

     
    /* Paste your code here (You may delete these lines if not writing code) */
     
  • Kedar

    Very well explained about the linkage topic.

    I would like to just provide a suggestion about the below description given in post:

    extern int var;
    int main(void)
    {
    var = 10;
    return 0;
    }

    Analysis: This program throws error in compilation. Because var is declared but not defined anywhere. Essentially, the var isn’t allocated any memory. And the program is trying to change the value to 10 of a variable that doesn’t exist at all.

    In the analysis it says this program throws error in compilation. However, if we analyze it correctly, this not a compile time error. This is an linker error. During compilation it only refers to the declaration of the variable and error is thrown at the time of linking because the memory is not allocated for the variable.

    Please correct me in case I’m wrong

    • Sunil

      Yes, as you say it should be linker error and not compilation error.

       
      /* Paste your code here (You may delete these lines if not writing code) */
       
    • prakash

      Absolutely agree… its a linker error not a compiler error

       
      /* Paste your code here (You may delete these lines if not writing code) */
       
  • http://akkjj@hehe.com Nups
     
    /* Awesome explanation */
     
  • nirav

    Thanks this helpful for understanding extern keyword ! nice

  • Vikrant
     
    extern int a=0;
     

    gives error in c/c++
    error:‘var’ has both ‘extern’ and initializer

    • http://ffjdfd Ravi

      TO turn off the error that you are getting do the following settings
      “export CFLAGS=”-Wno-error” and run gcc command. This time it just gives a warning not an error.

       
      /* Paste your code here (You may delete these lines if not writing code) */
       
  • deepak

    gud explanation .. can u please spot some light on difference between global variables and variable declared using extern.
    and if what is its advantage over global variable

     
    /* Paste your code here (You may delete these lines if not writing code) */
     
    • ravi

      global and extern are same. extern is used to refer global variables.

      • http://www.eespl.com Ashutosh Dabgotra

        No, they are not the same…
        i’ve explained it below.

    • http://www.eespl.com Ashutosh Dabgotra

      global variable can be used in all the functions in one file only,
      on the other hand an extern variable can be used in all the functions of all the files used in a project.

      eg. if a project named “Thumb_scanner_module” contains 6 files i.e. “main.c”, “finger.c”, “timer.c”, “UART.c”, “LCD.c” and “key.c”,
      Now you want to use a common variable in multi files, then you have to use extern variable.
      Say i’m using a variable named “Finger_buff[26]” having datatype unsigned char, and i need this variable in “main.c”, “finger.c” and in “timer.c”, then i need to define this variable in any of these file by using keyword “unsigned char Finger_buff[26];” say i defined in “main.c”. And then in rest of two files i.e “finger.c” and in “timer.c” i need to declare the variable by using key word “extern unsigned char Finger_buff[26]” Hence in this way the variable “Finger_buff[26]” can be used in any function of any of these three files.

      #Note that in any one of the file we need to define the variable, which can be done by writing
      “unsigned char Finger_buff[26];”
      not “extern unsigned char Finger_buff[26];”

      • Gopalakrishnan

        Very Nice Explanation…. Thanks

         
        /* Paste your code here (You may delete these lines if not writing code) */
         
  • a user

    Thank you

  • vikash

    very well explained.

  • vikash

    nice

     
    /* Paste your code here (You may delete these lines if not writing code) */
     
  • Vidya

    wonderful, first time I got to the right link in no time.

    • mahesh

      in other file is it necessary to declare variable as extern

       
      /* Paste your code here (You may delete these lines if not writing code) */
       
  • shazia
     
    /* Paste your code here (You may delete these lines if not writing code) */
    very nice explained...:)
     
  • Sourav Ghosh

    hello.

    if you try to declare a variable as extern and try to initialize at the same time, it will obvoiusly throw an error.

    want to try? check this.

    $ gcc –version
    gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-48)
    Copyright (C) 2006 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions. There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

    $ ldd –version
    ldd (GNU libc) 2.5
    Copyright (C) 2006 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions. There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    Written by Roland McGrath and Ulrich Drepper.

    the code for test1.c

     
    /* #include<stdio.h>
    
    int main(void)
    {
            extern int var=1;
            printf("\nThe value of var is %d\n",var);
            return 0;
    }
     */
     

    and the o/p at compilation time

    $ gcc test1.c -o t
    test1.c: In function ‘main':
    test1.c:6: error: ‘var’ has both ‘extern’ and initializer

    Please make me undersatnd. thank you.

  • nikita

    very good post

     
    /* Paste your code here (You may delete these lines if not writing code) */
     
    • jayant
       
      /**/
       
  • Jay

    Thank you so much. Your explanation was clear as crystal!

  • parashar

    well explained !!

  • satish baganal

    super explanation,thank you

     
    /* Paste your code here (You may delete these lines if not writing code) */
     
  • Subodh Kumar

    excellent explanation…!!!

  • alperenc

    Very informative. Always wondered what extern is.Thank you very much.

  • arpit

    excellent article….really enjoyed reading it.

  • prathibha

    i really liked it, very usefull to me… thanks a lot

     
    /* Paste your code here (You may delete these lines if not writing code) */
     
  • lee

    quote”remember that declaration can be done any number of times”
    how about redelcaration error ?
    I am confused here.
    Thanks

  • Alireza

    What is the “actual definition of the function”?

    int foo(int arg1,char arg2); //prototype declaration
    int foo(int arg1,char arg2){/*some codes*/}; //pseudo-definition
    int foo(int arg1,char arg2){/*some codes*/} //actual definition

    Am I right

    • Rakesh

      what is the purpose of psedo definition. Please let me know.
      Thanks in Advance

  • Aarthi

    Excellent explanation. Thank u :-)

  • Aarthi
     
    /* Paste your code here (You may delete these lines if not writing code) */
     
  • Hitesh

    great explanation! eye opener :)

     
    /* Paste your code here (You may delete these lines if not writing code) */
     
  • http://www.junwatu.com Eq

    Explained very well! Nice post!

  • parthi

    hi,
    this details easy 2 understand…
    great explanation

    thanku

    parthi

  • Rudra Pratap

    Excellent explanation. Thanks very much.
    God bless u

  • hitesh jhamb

    excellent…. :):)

  • hitesh jhamb

    excellentt… :):)

     
    /* Paste your code here (You may delete these lines if not writing code) */
     
  • vijay

    awesome work!

  • viewer

    Excellent explanation……..

     
    /* Paste your code here (You may delete these lines if not writing code) */
     
  • Nishant

    Good Explanation, Thanks For very informative explanation…KEEP IT UP

  • ashish

    Thanks dude i cleared a big confusion of mine

     
    /* Paste your code here (You may delete these lines if not writing code) */
     
  • Saurabh

    Thanks dude for the informative article. Things looks so simple when they are crystal clear

  • kannan

    extraordinary man!!!!

     
    /* Paste your code here (You may delete these lines if not writing code) */
     
  • suraj

    awesome man……………
    gr8 job………….

  • lalitha

    excellent.. cleared my doubts

  • kartik

    Very good and easy to read explanation , the examples were very helpful too .

  • Vivek Pant

    great…nicely explain…thankx :):):)

  • Amlan

    Example 3 for “extern variable” will give linking error, the explanation is correct though.

  • geeky_prep

    Great one..! Thanks

  • Pawan Thakur

    Super awesome explanation mahn you rock \m/

  • http://geeksforgeeks.org/archives/840 shri

    thanx !!!
    understood very well !!

  • abhishek

    thanks for ur perfect explanations …

  • Kavitha

    Thank you so much ! :)

  • Subi Puthalath

    Excellent !

  • Gaurav

    Excellent explaination with very good examples :-)

  • sailesh singh

    Brilliant very good post!!!
    KEEP UP THIS GOOD WORK!!!

  • http://chankeypathak.blogspot.com/ Chankey Pathak

    This is an interesting issue. For more issues like this refer to “C traps and pitfalls” it is awesome.

  • Anuj

    Hi,
    extern int x = 0;//this works only at global scope not in a local scope as we can do with functions.

  • Enisha

    Good explaination !! Thankx for the examples 2 :)

  • senthil

    hey nice work ! but extern int i=20;/* it will throw error ,i has both extern and initializer !

    • Rajeev

      No it will work fine.

       
      /* Paste your code here (You may delete these lines if not writing code) */
       
    • rocco

      try extern i=20 in global scope.it wont throw an error.
      An extern variable when defined can be accessed anywhere in a program.Since extern i=20 in a function means its lifetime is the scope of the function and it cannot be accessed from nywhere in program.Hence an error is thrown out of this ambiguity.

  • jasmeet

    brilliant

  • jasmeet

    it cleard my doubts.thanks

  • jhn

    needs more clearance in ur expl

  • Kohila Jaya

    Very nice Explanation…It is very useful.

  • ram

    brilliant job !!! :) Keep posting like this…

  • kapil gupta

    nice explanation !! but why this code not works.

    int main(void)
    {
    extern int var=1;
    cout<<var;
    return 0;
    }

    • kartik

      See @venki’s comment .

  • SHASHANK

    thanks nice article…..short,very clear n informative..

  • sidhu

    superb article….to the point, unlike some junk floating around in the form of articles….

  • RB

    extern int a = 0;
    throws an error, at least on gcc compiler it does. Also, it is not valid according to KnR.

    • http://math-puzzles-computing.blogspot.com/ Venki

      @RB, thanks for update. It is because extern int is declaration, not definition.

      • http://www.google.com priya

        But when we declare as “extern int a = 0;”
        it serves a s the definition also. So

         
        int main(void)
        {
        extern int var=1;
        cout<<var;
        return 0;
        }
        
         

        should not throw error.

  • wilson

    very nice explanation…keep it up

  • mohsen

    perfect explanations …

  • ruchi gaur

    awsome! ur explaination cleared all my doubts..thanks

  • ruchi gaur

    awsome! cleared all my doubts..thnks! :)

  • sekhar

    you geeks rock

  • ishant

    awesome……not words to says….god bless

  • Venki

    Good article. I hope the following link will be helpful to the readers of the article.

    http://www.eetimes.com/design/industrial-control/4007522/Linkage-in-C-and-C-

  • kishore

    very useful indeed :)

  • mani

    excellent!!!!!!

  • raj

    good one !!!