How is an external variable is accessed from one file to another?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rahul sinha
    New Member
    • Jan 2011
    • 17

    How is an external variable is accessed from one file to another?

    Can someone demonstrate how an external variable is accessed from one file to another? Also how do we use it when we have already defined the external variable in the source file?
    Last edited by Niheel; Jan 26 '11, 01:33 AM. Reason: Please use proper grammar and spelling. Making your question easily readable to everyone is the quickest way to get an answer.
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    you use the extern keyword to declate an external identifier, see this discussion on storage specifiers


    e.g. file data.c
    Code:
    int data;
    and test.c
    Code:
    extern int data;              // declare external identifier
    
    int main()
    {
        data = 10;                // use external identifier
        printf("%d\n", data);
    }
    using gcc compile it so
    gcc test.c data.c

    Comment

    • rahul sinha
      New Member
      • Jan 2011
      • 17

      #3
      okay i got that how to declare....but if i am defining a variable in one file say test.c and i want it to be used in other file..then in test.c,do i have to prefix extern before variable definition,,, like extern int a=20; ...and if not then howcome in any file making an extern declaration of varibale a will help me access the variable -a- of file test.c.........

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        you define the variable (allocate memory for it) in one file (and you can initialise it here)
        Code:
        int data=100;          // define the variable
        you then declare the variable in all other files that use it by prefixing it with extern (saying it is defined elsewhere)
        Code:
        extern int data;          // declare the variable
        you can only initailse the variable when you define it
        don't initialise the varaiable when you declare it, e.g.
        Code:
        extern int data=10;          // declare the variable
        otherwise the compiler with give warnings or error messages, e.g. from MPLAB
        Code:
        ADNS6090.c:60: warning: 'data' initialized and declared 'extern'

        Comment

        • rahul sinha
          New Member
          • Jan 2011
          • 17

          #5
          thank you sir....i am really grateful to you for solving my problems with such ease...can i have your id..i would like to learn more from you..(only if thats okay with you)....
          Last edited by Dormilich; Jan 26 '11, 12:28 PM. Reason: removed email address

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32636

            #6
            To save Horace1 the embarrasment of having to answer that, I will explain that such a request is considered quite inappropriate. Please do not make any such further requests.

            Comment

            • rahul sinha
              New Member
              • Jan 2011
              • 17

              #7
              okay,no problem,,,i will keep it in notice from next time.......

              Comment

              • rahul sinha
                New Member
                • Jan 2011
                • 17

                #8
                supppose i have defined a variable in some other file say external.c but i have not included that file in my present file test.c...and if i make an extern declaration of the same variable..then will i be able to access it...

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #9
                  "Including" is not important. Your files have to end up looking something like what horace1 showed you. It doesn't matter if you achieve that content with include files or by putting the statements directly into the c source file. Each of your c source files is compiled separately and individually into an object file. Then all of the object files are linked together. It is the link step that resolves references to global variables.

                  Comment

                  Working...