extern keyword

This is a discussion on extern keyword within the C Programming forums, part of the General Programming Boards category; hi, i'm puzzled by the use of the extern keyword. 1) when used in functions, it's just for stylistic purposes. ...

  1. #1
    Registered User trekker's Avatar
    Join Date
    Mar 2002
    Posts
    46

    extern keyword

    hi,

    i'm puzzled by the use of the extern keyword.

    1) when used in functions, it's just for stylistic purposes.
    So, that use is pretty clear.

    2) when used in variable declarations what's the purpose?

    e.g.

    test1.c
    Code:
    #include <stdio.h>
    
    int a; /* extern????? */
    /* extern int a; */
    
    void f1();
    
    
    int main() {
      a = -3;
      f1();
    
      printf( "a=%d\n", a );
    
      return 0;
    }

    test2.c
    Code:
    #include <stdio.h>
    
    int a;
    
    void f1()
    {
      printf( "a=%d\n", a );
      a=4567;
    }
    i can't see the difference.
    shouldn't the compiler give a warning/error when the keyword extern isn't used?
    or at least the two variables shouldn't affect each other.


    thanx in advance,
    trk
    Last edited by trekker; 06-01-2005 at 09:22 AM.
    to boldy code where...

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    #include <stdio.h>
    
    int a;
    
    void f1();
    
    
    int main() {
    Here it is also a definition.
    http://www.eskimo.com/~scs/C-faq/q1.7.html
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User trekker's Avatar
    Join Date
    Mar 2002
    Posts
    46
    u shouldn't be so hasty....
    that's not what i asked

    thanx anyway
    to boldy code where...

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by trekker
    u shouldn't be so hasty....
    that's not what i asked
    How about using preview instead of posting a question so hasty?

    And it will be a linker (not compiler) error if you have multiple definitions.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User trekker's Avatar
    Join Date
    Mar 2002
    Posts
    46
    sorry m8 i accidentally pushed the post button...

    i should hav known u r lurking...to get your post counter up...
    to boldy code where...

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    1

    Thumbs down Hi Trekker

    hi ,


    extern keyword is mean for linking

    for any variable local to a program giving it extern keyword means the storage is done in another
    file and if compiler cann't find it at link time it will report error

    bye

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by trekker
    sorry m8 i accidentally pushed the post button...

    i should hav known u r lurking...to get your post counter up...
    It is what you asked. It's answered in the link you were provided. For the record, none of the people that regular this site give a ........ about post count. All of the people with really high post counts have been around here for a very long time, helping ungreatful people like yourself.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    whenever you compile a file.c you'll get a n associated file.o or obj , which contains the compiled functions and simbols definitions. If you declare ' int a; ' you'll define that var in a object file. If you declare the same variable in another file, the linker when assembling your final executable with all your object files, will see that there are variables with equal type and name, so it'll report an error, because it doesn't know which variable to use.
    When you declare a variable as extern you're saying that you're going to use that variable in that file, but the variable is declared/stored wherelse. Then the linker will see that, in one file you declare the variable, and in another you refer to that same variable as extern (being in another object file).

    You can try this, and try to understand what the linker does.
    Declare the var in all files as extern - linker error, because it can't find the definition, only references to that variable that doesn't exist
    Define that same function in diferent files - you'll get symbol redifinition, because the linker has to choose one of the functions and doesn't know which.
    Declare only a function (the header) and try to use it - you'll get a link error because the function definition (body) wasn't compiled, therefore the linker can't find the function to use.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. extern classes
    By Swordsman in forum C++ Programming
    Replies: 1
    Last Post: 05-07-2008, 03:07 AM
  2. usage of extern keyword
    By ilikepure in forum C++ Programming
    Replies: 2
    Last Post: 09-26-2007, 07:24 PM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. extern keyword
    By Shadow12345 in forum C++ Programming
    Replies: 1
    Last Post: 05-26-2002, 01:33 PM
  5. extern keyword and structures
    By GuitGentlyWeeps in forum C Programming
    Replies: 2
    Last Post: 01-30-2002, 07:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21