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

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.