What is extern and how is it used?

This is a discussion on What is extern and how is it used? within the C++ Programming forums, part of the General Programming Boards category; I am not sure if the extern command is specifically C++ or C, but I know it work with C++. ...

  1. #1
    Unregistered
    Guest

    Question What is extern and how is it used?

    I am not sure if the extern command is specifically C++ or C, but I know it work with C++. I see it used a lot in the q3 coding and wondered what it did. Thanks for the help.

  2. #2
    edk
    edk is offline
    Registered User
    Join Date
    Sep 2001
    Posts
    20
    extern specifies that the variable is in another file.

  3. #3
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    first it works with C and C++.

    now as how to use it heres how

    say you have a variable(int k = 10) in mycpp1.cpp and you want to use it in mycpp2.cpp use the extern to redeclare the variable and use it.

    Example:
    Code:
    //mycpp1.cpp
    
    int k = 10;
    Code:
    //mycpp2.cpp
    
    extern int k;
    
    void function(void)
    {
        cout << k << endl;
    }
    this will print
    10
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    164

    Thanks!

    Thanks! That solved the only problem I had with my app.
    // Gliptic

Popular pages Recent additions subscribe to a feed

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