Thread: What does this statement mean?

  1. #1
    Registered User starcatcher's Avatar
    Join Date
    Feb 2008
    Location
    Australia
    Posts
    45

    What does this statement mean?

    I found this in the help files when I looked up 'extern'. What does it mean?
    Declarations of variables and functions at file scope are external by default.
    The blue bits are the bits that I don't understand.
    Thx,
    Philipp

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    File-scope means that the scope of the variable (where the variable can be used/seen) is the entire file - this means "global" variables in a file. Variables can have limited scope, e.g. be within a function or even a single block inside a function, eg.
    Code:
    int x;   // file scope
    
    int func1(int y)   // function scope parameter. 
    {
       int z;  // function scope local variable. 
    
       if (x == 1)
       {
           int w;   // Block scope
       }
       // w doesn't exist here. 
    }
    Edit: external means that the function/variable can be seen in another file, eg. you have "main.c" and "func.c", and declared x in main.c as above, then you can tell the compiler that there is an [code]extern int x;[/code} in func.c and it becomes the same variable as main.c. Functions don't need the extern keyword to make the function usable in another file, you just have to tell the compiler the prototype (what function name is and what the return type and parameters are) to use it.

    --
    Mats
    Last edited by matsp; 02-03-2008 at 10:46 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Just means that if you declare variables and functions in one compilation unit ( source file ) they are accessible from other compilation units by default.
    If you dont want them to be acessible from other compilation units, declare as static.
    Kurt

  4. #4
    Registered User starcatcher's Avatar
    Join Date
    Feb 2008
    Location
    Australia
    Posts
    45
    Thank you. Explains why there was no error when I used a function defined in another file... The declaration was in a header though which I included in the main file where I used the function.
    Philipp

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  2. Meaning of this statement?
    By @nthony in forum C Programming
    Replies: 7
    Last Post: 07-16-2006, 02:57 AM
  3. if/break statement
    By Apropos in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2005, 02:33 PM
  4. string & if statement
    By Curacao in forum C++ Programming
    Replies: 4
    Last Post: 05-02-2003, 09:56 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM