Thread: What is extern and how is it used?

  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
    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,263
    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