Thread: extern

  1. #1
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291

    extern

    what's the use of the extern on the statement?:
    >> extern int function (int *)

    what's the different between :
    1 - extern
    2 - register
    3 - ... and others.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    > >> extern int function (int *)
    The extern keyword in this declaration is redundant. Functions are extern by default, meaning that they can be accessed from other source files. The two following declarations are equivalent:

    extern int function ( int * );
    and
    int function ( int * );

    Now, you can restrict access for the function to the file it is declared in by declaring it as static:

    static int function ( int * );

    This means that the function can only be used in the file that it was declared in.

    Both extern and static have different meanings when used with variables in different scopes. A variable declared extern means that the variable was defined elsewhere and it can be used without an explicit definition in the source file where it is used. When static is used on a variable in global scope the meaning is the same as for a function, but when used in a local scope the variable will retain its value between function calls as if it were a global variable:
    Code:
    #include <stdio.h>
    
    void func ( void )
    {
      static int i = 0;
    
      printf ( "%d\n", i++ );
    }
    
    int main ( void )
    {
      func();
      func();
    
      getchar();
    
      return 0;
    }
    The output of this program is

    0
    1

    If i were not declared as static then the output would be

    0
    0

    because the variable would be redefined and initialized to 0 with each call. When declared static, i is only initialized once and retains any changes made to it between calls.

    The register keyword is a hint to the compiler that the variable will be used a great deal and the programmer thinks that it should be placed in a register for faster access. However, the compiler can and often does ignore this hint. Also note that a register variable may only be declared in local scope and you can't take its address.

    The auto keyword is completely redundant and shouldn't be used since it can only be used in local scope and all local variables are auto by default. This keyword is really only useful for compiler writers, so you can safely forget it exists.

    -Prelude
    My best code is written with the delete key.

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, 02:07 AM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. Odd Problem with Linking
    By jamez05 in forum C++ Programming
    Replies: 6
    Last Post: 09-21-2005, 01:49 PM
  4. Extern Question, really confused
    By SourceCode in forum C Programming
    Replies: 10
    Last Post: 03-26-2003, 11:11 PM
  5. extern symbols, what do these mean exactly?
    By Shadow12345 in forum C++ Programming
    Replies: 8
    Last Post: 11-02-2002, 03:14 PM