Extern in c/c++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chetanamale
    New Member
    • Sep 2007
    • 7

    Extern in c/c++

    Hello :

    Can anyone please elaborate in detailed use of extern in c/c++.The articles i read

    say " its used for those methods/functions defined somewhere else",

    e.g.extern 'function name', extern"C" etc..

    thanks,
    chetan.
  • deoashish
    New Member
    • Sep 2007
    • 2

    #2
    Originally posted by chetanamale
    Hello :

    Can anyone please elaborate in detailed use of extern in c/c++.The articles i read

    say " its used for those methods/functions defined somewhere else",

    e.g.extern 'function name', extern"C" etc..

    thanks,
    chetan.
    ----------------------------------------------------------------------------------------------------------------------

    When u code, your source code is distributed over multiple files. You compile them separately and link the object code. Now it may happen for instance say you have one variable that has been defined in one file and is used to keep count. Now in some other file you need to have a count. but then if you are including the same file in your project, then will you declare a variable called as count back again, although u have the variable already defined for that purpose.

    So in order to remove this wastage of memory, we tell the compiler that i need such variable. but this being already delcared somewhere... i also want to use it in over here. this is extern...

    Comment

    • chetanamale
      New Member
      • Sep 2007
      • 7

      #3
      deoashish, can u explain it with a code ?? anyway im not getting what you want to

      say, like suppose we have 1.c and 2.c both include 1.h.We have defined count

      variable in 1.h which is been included in both 1.c and 2.c , so at second place

      you will define count as "extern count " instead of including 1.h ???

      is that correct ?

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Originally posted by chetanamale
        say, like suppose we have 1.c and 2.c both include 1.h.We have defined count variable in 1.h
        You never define a variable in a header file. Let me repeat that: You never define a variable in a header file.

        If you do, then every time you include the header you get a anotrher variable. When your build gets to the link step, it will fail is redefinition errors.

        What your do is define the variable in a .c file:

        [code=c]
        //in a .c file:

        int var;
        [/code]

        To access this variable from another .c file you tell the compiler the variable is defined outside (external to) this file:
        [code=c]
        //another .c file.

        extern int var;

        [/code]

        All the header file does is save you from putting than extern in every .c file. Instead you put the extern in the header and then include the header.

        Comment

        • chetanamale
          New Member
          • Sep 2007
          • 7

          #5
          yes i understood.But have some more questions

          1) Anyway you are redefining variable again as extern int Var etc.. then whats

          the use of defining this variable as extern ( isn't it not as good as defining int Var) ?

          2) Do extern variables retain same value as originally defined variables ?

          Comment

          • gpraghuram
            Recognized Expert Top Contributor
            • Mar 2007
            • 1275

            #6
            Originally posted by chetanamale
            yes i understood.But have some more questions

            1) Anyway you are redefining variable again as extern int Var etc.. then whats

            the use of defining this variable as extern ( isn't it not as good as defining int Var) ?

            2) Do extern variables retain same value as originally defined variables ?
            If u are using extern in the variable declaration it is not definition.
            What you are doing is declaration of the variable , means definition of the variable is available elsewhere.

            Raghuram

            Comment

            Working...