Local vs Global Variables in C

Local vs Global Variables in C

In this article, I am going to discuss the Local vs Global Variables in C with examples. Please read our previous articles, where we discussed the Call by value and Call by reference in C. At the end of this article, you will understand what are Local Variables and Global Variables in C and what is the difference between them with examples.

Scope Rule:

In C, all variables have a defined scope. The region of the program over which the declaration of an identifier is visible is called the scope of the identifier. The scope relates to the accessibility, the period of existence, and the boundary of usage of variables declared in a statement block or a function.

Block Scope:  

This means that the identifier can only be used in the block in which it is declared. These variables are created at the point of their declaration inside the block and cease to exist outside it. Outside the block, these variables are unknown and non-existent. A program may also contain a nested block, like ifs and loops. Variables declared with the same names as those in the outer block, masks the outer block variables while executing the inner block. In nested blocks, variables declared outside the inner blocks are accessible to the nested blocks, provided these variables are not re-declared within the inner block.

Syntax:  

Block Scope

Example:
main()
{
 int i=10;
 {
  int j=20;		// j is created
  i++;		// I is incremented into 11
  j++;		// j is incremented into 21
 }			// j is destroyed
 printf(“%d”,i);		// output is 11
 printf(“%d”,j);		// it is error as j is already destroyed.
}

There are three places where variables can be declared in C programming language −

  1. Inside a function or a block which is called local variables.
  2. Outside of all functions which are called global variables.
  3. In the definition of function parameters which are called formal parameters.
Local Variables in C:

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. The following example shows how local variables are used. Here all the variables a, b, and c are local to the main() function.

Program to understand Local variables in C:
#include <stdio.h>
int main () 
{
  /* local variable declaration */
  int a, b;
  int c;
  /* actual initialization */
  a = 10;
  b = 20;
  c = a + b;
  printf ("value of a = %d, b = %d and c = %d\n", a, b, c);
  return 0;
}

Output: Local Variables in C

Global Variables in C:

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. The following program show how global variables are used in a program.

Program to understand Global Variables in C:
#include <stdio.h>
/* global variable declaration */
int g;
int main ()
{
  /* local variable declaration */
  int a, b;
  /* actual initialization */
  a = 10;
  b = 20;
  g = a + b;
  printf ("value of a = %d, b = %d and g = %d\n", a, b, g);
  return 0;
}

Output: Global Variables in C

Local and Global variable with the same name in C:

A program can have the same name for local and global variables but the value of the local variables inside a function will take preference.

#include <stdio.h>
/* global variable declaration */
int g = 20;
int main () 
{
  /* local variable declaration */
  int g = 10;
  printf ("value of g = %d\n", g);
  return 0;
}

Output: Local Vs Global Variables in C

Formal Parameters in C Function:

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

Program:

#include <stdio.h>
/* global variable declaration */
int a = 20;
int sum(int a, int b);
int main () 
{
  /* local variable declaration in main function */
  int a = 10;
  int b = 20;
  int c = 0;
  printf ("value of a in main() = %d\n",  a);
  c = sum( a, b);
  printf ("value of c in main() = %d\n",  c);
  return 0;
}
/* function to add two integers */
int sum(int a, int b) 
{
   printf ("value of a in sum() = %d\n",  a);
   printf ("value of b in sum() = %d\n",  b);
   return a + b;
}

Output:

Formal Parameters in C Function

Initializing Local and Global Variables in C

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.

Initializing Local and Global Variables in C

It is a good programming practice to initialize variables properly, otherwise, your program may produce unexpected results, because uninitialized variables will take some garbage value already available at their memory location.

What are formal arguments and actual arguments in C?

In function header or in function declarator whatever the variables we are creating those are called parameters or formal arguments. On the other hand, in function calling statement whatever the data we are passing those are called actual arguments or arguments.

In order to call any function if it is required specific no of parameters then we can’t call the function with less than or more than the required number of parameters. Where the implementation part of the function is given it is called function definition.

In function definition first line is called function declarator or function header. When we are providing the type information explicitly to the compiler then it is called declarator.

Declaration statement does not contain the body but it contains type information. Let us see a program for understanding this concept.

#include <stdio.h>
void abc(int x,  int y) //function declarator
{
    printf("x = %d y = %d", x, y);
}
int main()
{
    int a = 10, b = 20;
    abc(a, b);  //function calling
    printf("\na = %d b = %d", a, b);
}

Output:

What are formal arguments and actual arguments in C?

In the next article, I am going to discuss Recursive Functions in C with examples. Here, in this article, I try to explain Local Vs Global Variables in C. I hope you enjoy this Local Vs Global Variables in C article. I would like to have your feedback. Please post your feedback, question, or comments about this article

Leave a Reply

Your email address will not be published. Required fields are marked *