Thread: Question about "extern"-clarification

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Question Question about "extern"-clarification

    Hello.
    Taking the advice from someone on this message board, I have begun reading Bruce Eckel's book "Thinking in C++". And now, for the first time, I have come across mention of the keyword "extern". This is the first time I have run across it, and I have not seen it so far in any of the tutorials I have read on this site.
    Well, the author of this book claims you have to use this keyword everytime you want to declare a variable without defining it. Oddly enough, though, I have already declared many variables without defining them, without this keyword, and experienced no trouble. Is it really essential for variable declaration without immediate definition?

    For example, here is the some of the text concerning this that I found in this book:

    In a function declaration, you give a type (the return value), the function name, the argument list, and a semicolon. That’s enough for the compiler to figure out that it’s a declaration and what the function should look like. By inference, a variable declaration might be a type followed by a name. For example:

    int a;

    could declare the variable a as an integer, using the logic above. Here’s the conflict: there is enough information in the code above for the compiler to create space for an integer called a, and that’s what happens. To resolve this dilemma, a keyword was necessary for C and C++ to say “This is only a declaration; it’s defined elsewhere.” The keyword is extern. It can mean the definition is external to the file, or that the definition occurs later in the file.

    Declaring a variable without defining it means using the extern keyword before a description of the variable, like this:

    extern int a;

    extern can also apply to function declarations. For func1( ), it looks like this:

    extern int func1(int length, int width);

    This statement is equivalent to the previous func1( ) declarations. Since there is no function body, the compiler must treat it as a function declaration rather than a function definition. The extern keyword is thus superfluous and optional for function declarations. It is probably unfortunate that the designers of C did not require the use of extern for function declarations; it would have been more consistent and less confusing (but would have required more typing, which probably explains the decision).

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You'll have to give us a little more context for us to be sure we know what you're talking about. I'm willing to guess, though: if you mean something like this:
    Code:
    int a;
    a = 1;
    then you're not meeting the criteria. The "int a" in the above code declares and defines the variable a. The "a = 1" is just an assignment to an already-declared-and-defined variable.

  3. #3
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by tabstop View Post
    You'll have to give us a little more context for us to be sure we know what you're talking about. I'm willing to guess, though: if you mean something like this:
    Code:
    int a;
    a = 1;
    then you're not meeting the criteria. The "int a" in the above code declares and defines the variable a. The "a = 1" is just an assignment to an already-declared-and-defined variable.
    Then if that's the case, and "int a" both declares and defines "a" at the same time (yes, I was thinking what you call "assignment" means "defines"), then why use "extern" at all, as according to the author of this book, its for when you declare a variable without defining it?
    Declaring a variable without defining it means using the extern keyword before a description of the variable, like this:

    extern int a;
    Last edited by Programmer_P; 06-27-2009 at 10:47 PM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Programmer_P
    Then if that's the case, and "int a" both declares and defines "a" at the same time (yes, I was thinking what you call "assignment" means "defines"), then why use "extern" at all, as according to the author of this book, its for when you declare a variable without defining it?
    Because if you want to refer to a global variable in multiple source files, you would not be able to define it in each source file (one definition rule). As such, you must declare, but not define, it in each source file (e.g., by declaring it in a header that is included), except for that one source file where it is actually defined.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you want to use the same variable as is used elsewhere. For instance, when you get to the point that you have your source code in more than one data file, and you have a variable a in file code1.cpp, and you want to use that same variable in code2.cpp, you can't say "int a" as you'll get a brand new variable a. So you say "extern int a" instead. (Note that I say nothing about whether this is a good design issue; it may not be.)

  6. #6
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Question

    Quote Originally Posted by laserlight View Post
    Because if you want to refer to a global variable in multiple source files, you would not be able to define it in each source file (one definition rule). As such, you must declare, but not define, it in each source file (e.g., by declaring it in a header that is included), except for that one source file where it is actually defined.
    Ok, that makes sense, but the author of the book also says the following concerning the "extern" keyword:
    To resolve this dilemma, a keyword was necessary for C and C++ to say “This is only a declaration; it’s defined elsewhere.” The keyword is extern. It can mean the definition is external to the file, or that the definition occurs later in the file.
    Here, the part at issue is the statement I marked in bold:

    "It can mean that the definition is external to the file, or that the definition occurs later in the file."

    So how do you explain that? Why would you want to use it at all in that circumstance (i.e. where the defining of the variable is done in the same file)?
    Last edited by Programmer_P; 06-27-2009 at 10:56 PM.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Programmer_P View Post
    Ok, that makes sense, but the author of the book also says the following concerning the "extern" keyword:

    Here, the part at issue is the statement I marked in bold:

    "It can mean that the definition is external to the file, or that the definition occurs later in the file."

    So how do you explain that?
    Code:
    class X {
        int this, that, the_other;
    };
    
    extern X bob; //no bob yet!
    
    //stuff that can talk about bob even though bob doesn't exist
    //stuff that can talk about bob even though bob doesn't exist
    //stuff that can talk about bob even though bob doesn't exist
    
    X bob; //now there really is a bob

  8. #8
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Talking

    Quote Originally Posted by tabstop View Post
    Code:
    class X {
        int this, that, the_other;
    };
    
    extern X bob; //no bob yet!
    
    //stuff that can talk about bob even though bob doesn't exist
    //stuff that can talk about bob even though bob doesn't exist
    //stuff that can talk about bob even though bob doesn't exist
    
    X bob; //now there really is a bob
    But what is the point of doing that at all? Why not just do this:

    Code:
    class X {
        int this, that, the_other;
    };
    
    X bob; //bob is created
    
    //stuff that can talk about bob because bob DOES exist
    //stuff that can talk about bob because bob DOES exist
    //stuff that can talk about bob because bob DOES exist
    
    //no need for this line now: X bob; //now there really is a bob

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Programmer_P
    But what is the point of doing that at all? Why not just do this:
    Suppose that the extern variable declaration was in a header file:
    Code:
    #ifndef HEADER_H
    #define HEADER_H
    
    extern int foo;
    
    #endif
    Now, suppose that this header was included in the source file that defines this global variable:
    Code:
    #include "header.h"
    
    int foo;
    After macro replacement, we get a translation unit that is something like this:
    Code:
    extern int foo;
    
    int foo;
    Not particularly useful when you just look at it like this, but it means what it means. The variable was declared but not defined on the first line, and then defined on the third line.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Programmer_P View Post
    But what is the point of doing that at all? Why not just do this:

    Code:
    class X {
        int this, that, the_other;
    };
    
    X bob; //bob is created
    
    //stuff that can talk about bob because bob DOES exist
    //stuff that can talk about bob because bob DOES exist
    //stuff that can talk about bob because bob DOES exist
    
    //no need for this line now: X bob; //now there really is a bob
    There's not much of a point, unless you remember how #include works -- remember, include works just by simply dumping your header file in the top of your source code. This means that your same header file that you were using for all your other files works for the one file that contains your real definition of bob.

    (There might be an actual reason for you to use it "straight", but I never have and I can't come up with one at this point in time.)

  11. #11
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Well, I still don't understand why you would use it in the same file as the one you define it. I see why you would want to use it though in a header file, i.e. so that the variable doesn't get created in memory a long time before its used (as it would if you defined it in a header file, due to the included file's code being pasted at the very top of the main source code file to be used by the preprocesser), and so that you can use the same variable for multiple files, since it hasn't been defined in the header file.

    Thanks for the replies, btw.
    Last edited by Programmer_P; 06-27-2009 at 11:26 PM.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Programmer_P
    Well, I still don't understand why you would use it in the same file as the one you define it.
    Do you understand what tabstop and I have just explained? If you do, but want to know why one would manually define a variable as extern in the same file as one defines it, then read further and post the answer. If Eckel does not explain himself, I daresay that he means what we have explained.

    Quote Originally Posted by Programmer_P
    I see why you would want to use it though in a header file, i.e. so that the variable doesn't get created in memory a long time before its used (as it would if you defined it in a header file, due to the included file's code being pasted at the very top of the main source code file to be used by the preprocesser)
    A global variable has static storage duration, so its lifetime is pretty much the duration of the program anyway.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by laserlight View Post
    Do you understand what tabstop and I have just explained?
    Yes, I understand it, but to be honest, it didn't fully answer the question...hence why I asked again.
    Basically, you told me about a case of where someone would declare a variable in a header file without defining it there, and then include that header file in the source code file, where he would then define it. But, my question was, why (not how) would anyone want to declare a variable in the main source code file without defining it, and then later define it in the same file? That was my question.
    If you do, but want to know why one would manually define a variable as extern in the same file as one defines it, then read further and post the answer. If Eckel does not explain himself, I daresay that he means what we have explained.
    I have read further, and he ended the dialogue like so:

    extern can also apply to function declarations. For func1( ), it looks like this:

    extern int func1(int length, int width);

    This statement is equivalent to the previous func1( ) declarations. Since there is no function body, the compiler must treat it as a function declaration rather than a function definition. The extern keyword is thus superfluous and optional for function declarations. It is probably unfortunate that the designers of C did not require the use of extern for function declarations; it would have been more consistent and less confusing (but would have required more typing, which probably explains the decision).

    Here are some more examples of declarations:

    Code:
    //: C02:Declare.cpp
        // Declaration & definition examples
        extern int i; // Declaration without definition
        extern float f(float); // Function declaration
    
        float b;  // Declaration & definition
        float f(float a) {  // Definition
          return a + 1.0;
        }
    
        int i; // Definition
        int h(int x) { // Declaration & definition
          return x + 1;
        }
    
        int main() {
          b = 1.0;
          i = 2;
          f(b);
          h(i);
        } ///:~
    In the function declarations, the argument identifiers are optional. In the definitions, they are required (the identifiers are required only in C, not C++).
    So, as you can see, he still does not explain why one would want to define a variable (after declaring it) later on in the same file.
    A global variable has static storage duration, so its lifetime is pretty much the duration of the program anyway.
    Yes, but the variable would only be created in memory once the code "int a" was executed (which depending on the amount of lines in the main source code file, and flow of the program, might happen close or not-so-close to when the variable was actually used for something), and so the variable might be created in memory much later if it were defined in the main source code file, and was declared in the included header file.
    Last edited by Programmer_P; 06-27-2009 at 11:52 PM.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Programmer_P
    why would anyone want to declare a variable in the main source code file without defining it, and then later define it in the same file?
    As far as I know, there is no reason other than "for an academic or pedagogical example" and "because I am dumb and want to confuse my readers".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by laserlight View Post
    As far as I know, there is no reason other than "for an academic or pedagogical example" and "because I am dumb and want to confuse my readers".
    Ok, thanks. That explains it all. :laugh:

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 01:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM