C Variable Scope

Variable scope is an area of a program where variables can be accessed.

Two types of variables depending on a variable scope.

  • local scope variable aka. local variable
  • global scope variable aka. global variables

Local variables

  • The variable declared within parentheses of function or a block is called a local variable.
  • These local variables are stored in stack section in memory.
  • The scope and lifetime of a local variable are within the block/function.
  • The default/initial value of a local variable is garbage.
  • A local variable has higher priority than a global variable.

Output:

Global variables

The variables declare above the main() function are called as global variables

These global variables are stored in the data section of memory.

The scope and lifetime of a global variable are throughout the program.

The default/initial value of a global variable is zero.

The global variable should be initialized at the time of declaration.

Output:

Some more examples –

  • A local variable has higher priority than a global variable.

    Output
  • Constant local variable should be initialized at time of declaration.
  • The global variable should be initialized at the time of declaration.
Help others by sharing the content!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.