Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

External Variable

ishandoshiishandoshi Posts: 10Member
Is external variable globally accessible by all functions?
When it is declared within a function,does it have a declaration 'extern' associated with it?
If it is not initialized,is the default value 0?

Comments

  • dennisparkerdennisparker Posts: 478Member
    : Is external variable globally accessible by all functions?
    : When it is declared within a function,does it have a declaration 'extern' associated with it?
    : If it is not initialized,is the default value 0?
    :

    The external variable is globally accessible to all functions, unless the "static" keyword is accociated with it. A static external variable is only available to those functions within the file that it is declared, and only to those functions that come after the declaration within that file.

    You would only use the extern keyword within a function to let that function know to look for that variable elsewhere - for instance a global variable declared in another file.

    I do not believe that any variables are initialized by default.
  • stoberstober Posts: 9,765Member ✭✭✭
    [b][red]This message was edited by stober at 2005-8-16 10:19:25[/red][/b][hr]
    :
    : The external variable is globally accessible to all functions, unless the "static" keyword is accociated with it. A static external variable is only available to those functions within the file that it is declared, and only to those functions that come after the declaration within that file.
    :


    [b]extern[/b] keyword normally means that the variable was declared in some other *.c or *.cpp file. The [b]static[/b] keywords means that the variable is not visible outside the current *.c or *.cpp file. So the two keywords cannot be used with the same variable -- either it is visible outside the current file or it isn't, it cannot be both at the same time.

    All global variables are initialized to 0 by default.






  • dennisparkerdennisparker Posts: 478Member
    : [b][red]This message was edited by stober at 2005-8-16 10:19:25[/red][/b][hr]
    : :
    : : The external variable is globally accessible to all functions, unless the "static" keyword is accociated with it. A static external variable is only available to those functions within the file that it is declared, and only to those functions that come after the declaration within that file.
    : :
    :
    :
    : [b]extern[/b] keyword normally means that the variable was declared in some other *.c or *.cpp file. The [b]static[/b] keywords means that the variable is not visible outside the current *.c or *.cpp file. So the two keywords cannot be used with the same variable -- either it is visible outside the current file or it isn't, it cannot be both at the same time.
    :
    : All global variables are initialized to 0 by default.


    Interesting to learn they are initialized automatically.

    I did not mean to imply that the extern keyword was used in conjunction with the static keyword, only that the static keyword is used in conjunction with variables of storage class extern (globals).

    Small point on static external variables, I believe they are only visible to functions within the file if they are defined after the static external variable is declared, so the static external variable is not visible to functions within the same file if they were defined previously.
  • DonotaloDonotalo Posts: 715Member
    : Interesting to learn they are initialized automatically.
    :
    : I did not mean to imply that the extern keyword was used in conjunction with the static keyword, only that the static keyword is used in conjunction with variables of storage class extern (globals).
    :
    : Small point on [red]static external[/red] variables, I believe they are only visible to functions within the file if they are defined after the [red]static external[/red] variable is declared, so the [red]static external[/red] variable is not visible to functions within the same file if they were defined previously.
    :

    shouldnt they be [blue]static global[/blue]? i dont think that there is any variable called [red]static external[/red]!
  • stoberstober Posts: 9,765Member ✭✭✭
    : :
    :
    : shouldnt they be [blue]static global[/blue]? i dont think that there is any variable called [red]static external[/red]!
    :


    That's what I tried to say too. static and extern are mutually exclusive terms.

  • LundinLundin Posts: 3,711Member
    : : [b][red]This message was edited by stober at 2005-8-16 10:19:25[/red][/b][hr]
    : : :
    : : : The external variable is globally accessible to all functions, unless the "static" keyword is accociated with it. A static external variable is only available to those functions within the file that it is declared, and only to those functions that come after the declaration within that file.
    : : :
    : :
    : :
    : : [b]extern[/b] keyword normally means that the variable was declared in some other *.c or *.cpp file. The [b]static[/b] keywords means that the variable is not visible outside the current *.c or *.cpp file. So the two keywords cannot be used with the same variable -- either it is visible outside the current file or it isn't, it cannot be both at the same time.
    : :
    : : All global variables are initialized to 0 by default.
    :
    :
    : Interesting to learn they are initialized automatically.
    :


    And so are static variables, they are also set to 0 by default no matter where they are declared.
  • dennisparkerdennisparker Posts: 478Member
    [b][red]This message was edited by dennisparker at 2005-8-17 8:10:38[/red][/b][hr]
    : : Interesting to learn they are initialized automatically.
    : :
    : : I did not mean to imply that the extern keyword was used in conjunction with the static keyword, only that the static keyword is used in conjunction with variables of storage class extern (globals).
    : :
    : : Small point on [red]static external[/red] variables, I believe they are only visible to functions within the file if they are defined after the [red]static external[/red] variable is declared, so the [red]static external[/red] variable is not visible to functions within the same file if they were defined previously.
    : :
    :
    : shouldnt they be [blue]static global[/blue]? i dont think that there is any variable called [red]static external[/red]!
    :


    This is a matter of semantics :-), Every variable in C has two attributes: type and storage class. The 4 storage classes are: automatic, external, register, and static. The common usage of the word "global" refers to a variable declared outside of a function, and these variables are by default of storage class external.

    This is not the same thing as the [blue]keyword[/blue] "extern" which is used to inform the compiler to look elsewhere for the variable (which of course would be of storage class "external").

    My reference is "A Book on C" (Kelley, Pohl). Chapter 5.12 is titled "Static External Variables". Again, they are refering to storage class, and not the "extern" keyword.

    If you do a Google search on "Static External Variables" you will get multiple references.

Sign In or Register to comment.