This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

static == extern ?

extern data and static diffenece

since static is used for keeping the value after existing function, extern use the value in other module.

are they the same?

  • "extern ... and static ... are they the same?"

    Definitely not!

    A 'static' identifier cannot be accessed via an 'extern' declaration!

    Time to re-read your 'C' textbook.

  • If you want a variable that can be accessed in different modules, you have to declare it globally and not in a function.

  • I think the "they are the same" comes from the fact that both do not overlay. Otherwise, of course, they are quite different.

    Erik

  • "both do not overlay"

    Hmm... not really.

    That deduction is not logical, Captain:

    It's not the 'extern' that affects whether something can overlay or not - the thing has to be global before it can be accessed via 'extern', and it's the fact of being global that makes it non-overlayable.

  • sorry, I was a bit off, read my post as to "static" and "global". :(

    Of course, if something is "extern" it must be global, so thus "extern" makes sense. I do agree that a variable can be global without being used extern, but why the heck would anyone do that?

    Erik

  • "I do agree that a variable can be global without being used extern, but why the heck would anyone do that?"

    In 'C', everything defined at file scope is automatically "Public" - whether or not any other module actually references it.
    You use 'static' with file-scope objects if you want to prevent them from being "Public"

  • I do agree that a variable can be global without being used extern, but why the heck would anyone do that?

    So that you can watch that variable from outside the scope of the file in which it is declared. If the variable isn't global, some debuggers can't watch it if it is out of scope (if you're not debugging some code in that source file).

    Of course, not all debuggers work this way.

    Jon

  • "If the variable isn't global, some debuggers can't watch it if it is out of scope"

    In uVision, to watch a function's static local variable from outside the function, you have to give the fully-qualified name like "\\module\function\fred" instead of just "fred"

    I presume it would be similar for file-scope objects declared 'static'?

  • "In uVision, to watch a function's static local variable from outside the function, you have to give the fully-qualified name like '\\module\function\fred' instead of just 'fred'"

    Having just been trying to watch a 'static' local, I now remember that it's worse than that:

    Actually, you always have to give the fully-qualified name - whether or not the variable is currently in-scope.

    This is a pain, as it means that the pop-up display of the current value doesn't work, the variable doesn't appear in the 'locals' window, and you can't add it to a Watch window by right clicking. :-(

    PS

    The syntax is actually '\module\function\fred' - only a single backslash to start