Difference between Global and Static global variable. - Programmers Heaven

Howdy, Stranger!

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

Categories

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.

Difference between Global and Static global variable.

shantanukshantanuk Posts: 13Member
Hi,
What is the difference between global and static global variables?
_shantanuk.

Comments

  • stephlstephl Posts: 422Member
    : Hi,
    : What is the difference between global and static global variables?
    : _shantanuk.
    :
    Global variables are variables defined outside of any function. Their scope starts at the point where they are defined and lasts to the end of the file. They have external linkage, which means that in other source files, the same name refers to the same location in memory.
    Static global variables are private to the source file where they are defined and do not conflict with other variables in other source files which would have the same name.

    Steph
  • LundinLundin Posts: 3,711Member
    : : Hi,
    : : What is the difference between global and static global variables?
    : : _shantanuk.
    : :
    : Global variables are variables defined outside of any function. Their scope starts at the point where they are defined and lasts to the end of the file. They have external linkage, which means that in other source files, the same name refers to the same location in memory.
    : Static global variables are private to the source file where they are defined and do not conflict with other variables in other source files which would have the same name.
    :
    : Steph
    :


    As a sidenote, both global and static variables have static initialization, which means that if you don't set them to a value, they will be initialized to 0 (common variables) or NULL (pointers). This is the only case in ANSI C where you can assume that the value is zero without initializing it explicitly.

Sign In or Register to comment.