#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0

    Extern variables


    I have two files file1.c and file2.c both have common global variable as int a;..Now when I include them both in my file then compiler considers only the first one...This is due to tentative definitions in c..But i have heard to avoid this you can use extern in one of the variables..Can someone please explain how can this help??
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2009
    Posts
    45
    Rep Power
    7
    Read more about declarations and definitions. There's an example of extern keyword use.
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    In the header file, declare that variable as extern. That will inform every source file that #include's that header about the existence of that variable but without actually creating it. Then in one and only one of the source files, actually declare that variable.

    That's all it takes.

    Just a minute ... Just a minute ...

    Originally Posted by ghost_rider
    I have two files file1.c and file2.c both have common global variable as int a;..Now when I include them both in my file ...
    Are you including source files into another source file? Ie, is this what you're doing?
    Code:
    #include "file1.c"
    #include "file2.c"
    
    int main()
    {
    ...
    If that is what you're doing, then stop that idiocy right now! Use proper procedures for multiple file projects and only #include header files.

    Comments on this post

    • eramit2010 agrees

IMN logo majestic logo threadwatch logo seochat tools logo