Showing posts with label formal. Show all posts
Showing posts with label formal. Show all posts

Tuesday, 11 October 2016

Variable Scope in C Programming

A scope in any programming is a region of the program where a defined variable can have its existence and beyond that variable it cannot be accessed.


  • Inside a function or a block which is called local variables,
  • Outside of all functions which is called global variables.
  • In the definition of function parameters which are called formal parameters.

Local Variable 

Variables that are declared inside a function or block are called local variables. They can be used only by statements that are inside that function or block of code. Local variables are not known to functions outside their own.

Global variables



Global variables are defined outside a function, usually on top of the program. Global variables hold their values throughout the lifetime of your program and they can be accessed inside any of the functions defined for the program.
A global variable can be accessed by any function. That is, a global variable is available for use throughout your entire program after its declaration.

Formal parameters

Formal parameters are treated as local variables with-in a function and they take precedence over global variables.

 

 initialized  Local and Global Variables

When a local variable is defined, it is not initialized by the system, you must initialize it yourself. Global variables are initialized automatically by the system when you define them.