Results 1 to 7 of 7

Thread: [Resolved] confused about extern

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2005
    Posts
    530

    Resolved [Resolved] confused about extern

    Hi,
    First off, I have had no formal C training. I learned what I know of C from a book, "Absolute Beginner's Guide to C" and picked up other bits here and there. Also, my application is embedded, not run on a PC. I've written maybe a dozen programs for embedded applications over the last 2 or 3 years.

    My question is on the use of the word extern in a declaration. I'm confused about how and when its used. I understand that it is used for variables that are not local to a function. My confusion comes from having used global variables for some time now and I've never needed to use it.

    Usually, when I need global variables, I declare them before Main, outside of any function. If the program is large and I need many of them, I put them into a header file and include the header file in the program before the Main function. My understanding is that extern would be used before global variables that are used in other code files, but I put the declaration in a separate file (header file) and include it and it works without error.

    I'm wondering if my confusion stems from the fact that when I include all my code files into main, that the compiler makes one big file out of them, and so I don't need extern? Perhaps, if I didn't include the code files, but just listed them as part of the project, I would need extern? I would appreciate any clarity that anyone could give.

    Thanks.
    Last edited by rickford66; Apr 27th, 2011 at 09:31 AM.

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: confused about extern

    When you define a variable with the extern keyword, you are saying that the variable is actually declared elsewhere.
    The linker will attempt to find the actual declaration in the object files that are linked.

    Take a look at this example:

    main.c
    Code:
    #include <stdio.h>
    
    extern int foo;
    void bar();
    
    int main(int argc, char **argv)
    {
    	foo = 5;
    	bar();
    	printf("&#37;i", foo);
    	return 0;
    }
    foobar.c
    Code:
    int foo;
    
    void bar()
    {
    	foo = 10;
    }
    If these two files are built and linked, you will find that the 'foo' defined in main.c refers to the one declared in foobar.c.
    This is actually what happens with function definitions aswell. Notice that in main.c we have defined void bar(), but placed the function declaration in foobar.c. It is the exact same phenomena, only that function definitions are extern by default.
    These two lines are therefore identical:
    Code:
    void bar();
    extern void bar();
    I'm not very good at explaining, but I hope it made it somewhat clearer.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    May 2005
    Posts
    530

    Re: confused about extern

    If I rewrote it like this, I wouldn't need to use extern?

    main.c
    Code:
    #include <stdio.h>
    
    // extern int foo; <- extern no longer required?
    int foo; // new declaration
    void bar();
    
    include "foobar.c" // <- pastes foobar.c text here
    
    int main(int argc, char **argv)
    {
    	foo = 5;
    	bar();
    	printf("%i", foo);
    	return 0;
    }
    foobar.c
    Code:
    // int foo; <-not required anymore because this code "included" into main.c?
    
    void bar()
    {
    	foo = 10;
    }

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: confused about extern

    Exactly, because then it would no longer be a variable definition but a variable declaration.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    May 2005
    Posts
    530

    Re: confused about extern

    So when I use an include, as I did above, the linker is not involved because the include causes the included code to be pasted in and so it is treated as one big file?

    When I don't use include, but instead select files in the project, I have to use extern to use variables across files? The linker links up the variables so they are all the same instance of the variable?

    Do I have this right?

  6. #6
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: confused about extern

    When including a header, the preprocessor will just copy and paste the header contents into your source file, just like you say.

    It seems like you've got it right!
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    May 2005
    Posts
    530

    Re: confused about extern

    Ok, thanks. One more question. If foo is defined as an external, then foo can be defined in multiple files of program code and the linker will link them all together such that they are the same instance?
    Last edited by rickford66; Apr 27th, 2011 at 09:35 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width