Local vs Global Variables in C

Scope Rule in C Language:

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 in C Language:  

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.

Block Scope Syntax in C Language:  
Block Scope in C Language
Program to Understand Block Scope in C Language:
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 Language:

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.

Example to Understand Local Variables in C Language:
#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: Example to Understand Local Variables in C Language

Global Variables in C Language:

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.

Example to Understand Global Variables in C Language:
#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: Example to Understand Global Variables in C Language

Local and Global Variables with the same name in C Language:

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 with examples

Formal Parameters of a Function in C Language:

The Formal Parameters in C Programming Language are treated as local variables within a function and they take precedence over the global variables if any.

Program to Understand Formal Parameters of a Function in C Language:
#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:
Program to Understand Formal Parameters of a Function in C Language
Initializing Local and Global Variables in C Language

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. The following image shows the default of the data type.

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 Language?

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 Language
Follow Us On