Scope of Local and Global Variables in C Programming

The scope of variables refers to that portion of the program where the variables can be accessed. They are accessible in some portion of the program and in the other they are not accessible. Scope of a variable defines the portion of the program in which the set of variables can be referenced and manipulated.

When a variable is required in a program, it can be declared as:

  • Local variable
  • Global variable

Local Variables in C Programming Language

  1. The variables that are declared inside a function are called as local variables
  2. The scope is only within the function in which they are declared
  3. Local variables cannot be accessed outside the function in which it is declared
  4. Local variables exist in the memory only till the function ends
  5. The initial values of local variables are garbage values

In the above example,
Variables iNumber1 and iNumber2 are local to function main
Variable iResult is local to function fnSumPrint

Global Variables in C Programming Language

  1. The variables that are declared outside all the functions (Ex: above main()) are called as global variables
  2. These variables can be accessed by all the functions
  3. The global variables exist for the entire life-cycle of the program
  4. The global variables are initialized to default value
  5. Coding Standard for defining global variable:
    Each global variable should start with the alphabet g
    Example:
    int giValue;
    float gfSalary

giNumber1 and giNumber2 are global variables
Variable iResult is local to function fnSumPrint

Difference between Local and Global variables

Variable Scope Value after declaration
Local Variable Can be referenced only within the function in which it is declared Garbage value
Global Variable Can be referenced in all the functions Default values

Disadvantages of Global Variables

  • Lifetime of global variables is throughout the program. Hence usage of global variables leads to wastage of memory
  • Scope of the global variable is throughout the program. Hence more than one function can modify the value of the global variable. This makes debugging difficult.

How long do variables last?
By default, local variables (those declared within a function) have automatic duration: they spring into existence when the function is called, and they (and their values) disappear when the function returns.
Global variables, on the other hand, have static duration: they last, and the values stored in them persist, for as long as the program does. Of course, the values can in general still be overwritten, so they don’t necessarily persist forever.

What is the output of the following code?

The output:

Coding Standards for Writing Functions

Whether its C programming language or C++ programming language or PHP, we should have a coding standard in place before starting with the code development. Coding standard is a predefined standard which should be followed during the code development which will improve the quality of the code.

The coding standards varies from organization to organization. Each organization define their own coding standard and follow them to achieve the desired code quality.

Here, we will define a coding standard for writing functions.

Coding Standards for Writing Functions

  • A function name should be preceded by fn
  • The first character in the function name should be written in uppercase. Every subsequent word in the function name should start with an uppercase alphabet.

    Example:
    fnFactorial
    fnItemDisplay

  • The function should begin with a header which describes about the function. It is written as follows:

How does using functions make writing large programs easier?

Consider a program with 1000 lines. It is much easier to write a properly designed program consisting of 20 50-line functions than it is to write a program consisting of one 1000-line main program.

Functions can be written, tested, and understood independently of one another. You can write a function, test it, and convince yourself that it is correct without worrying about the other functions in your program. It is much easier to get your mind around a 50-line thing than a 1000-line thing.

Another benefit is that code can be reused between projects. If someone has previously written a function that you would like to use, it is a simple matter to incorporate it into your program. This is more so in domain specific functions, ex. Banking.

A third benefit is that the use of functions can eliminate the duplication of code. If the duplicated code is long enough, the use of functions can actually shorten a program.

A multi-person programming project proceeds by first deciding what functions need to be implemented, and then implementing the functions. Once the functions are designed, they can be divided up among the available programmers and implemented separately. Compare that with having multiple programmers work on a single main program.