Thread: extern usage

  1. #1
    Registered User
    Join Date
    Aug 2023

    Talking extern usage

    Without the static line in file1.cpp being commented out I get 300 201 300. So the extern in the function is not being used but the static variable is instead. If I do comment out the static line I get 300 301 301 which is what I expected. Why does the static declaration override the extern?
    maxcy / wt1v
    Attached Files Attached Files

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    I don't have references to the standard for you, but here is my reasoning.

    The extern variable declaration for "sameName" within "increment()" does not shadow the static "sameName". So, when the static global-scoped "sameName" is included in the program, "increment()" doesn't use the extern "sameName" at all.

    Think about it this way. Try keeping the static "sameName" in the program, but lift the "extern" declaration of "sameName" out of "increment()" and into the global scope, as if you'd included a header file. The compiler will complain that "sameName" is declared twice: one as static and once not as static.

    I don't know how local declarations of externally linked variables are supposed to interact with static definitions having the same name.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 04-13-2020, 09:48 PM
  2. Replies: 9
    Last Post: 05-07-2017, 12:29 PM
  3. how extern is extern?
    By davo666 in forum C Programming
    Replies: 11
    Last Post: 03-05-2009, 04:45 AM
  4. usage of extern keyword
    By ilikepure in forum C++ Programming
    Replies: 2
    Last Post: 09-26-2007, 06:24 PM
  5. Replies: 17
    Last Post: 12-15-2006, 11:02 AM

Tags for this Thread