Type of Variable and their Scope in C Programming Language

Rumman Ansari   Software Engineer   2022-12-13   13714 Share
☰ Table of Contents

Table of Content:


The C programming language defines the following kinds of variables:

  1. Constant Variables
  2. Volatile Variables
  3. Local Variable
  4. Global Variable

Constant Variables

C variables having same or unchanged value during the execution of a program are called constant variables. A variable can be declared as constant using keyword const. For example,

Syntax

const int a=100;

Now, if we try to change its value, then it is invalid.

Volatile Variables

Those variables that can be changed at any time by some external sources from outside or same program are called volatile variables. Any variable in c can be declared as volatile using keyword volatile.

Syntax

volatile data_type variable_name;

NOTE: If the value of a variable in the current program is to be maintained constant and desired not to be changed by any other external operation, then the variable declaration will be volatile const d=10;

Global Variables

Global variables in c have their declaration outside the function definition of all functions used with in the program and remains in the memory as long as the program is executing.

All global variables in c could be accessed from anywhere in program which means that any expression in the program can access the global variable regardless of what block that expression is written.

Thus, the global variables have their scope global.

Program

/*Program to illustrate c global variables*/

#include
int a=5; // global variables
void main()
{
    ++a;
    printf("%d \n",a);
    increment();
    return 0;
}
void increment()
{
    ++a;
    printf("%d \n",a);
}

Output

6
7
Press any key to continue . . .

Example program for global variable in C:

  • The scope of global variables will be throughout the program. These variables can be accessed from anywhere in the program.
  • This variable is defined outside the main function. So that, this variable is visible to the main function and all other sub functions.

Program

#include
void test();
int m = 21, n = 42;
int a = 55, b = 110;
 
int main()
{
   printf("All variables are accessed from main function");
   printf("\nvalues: m=%d:n=%d:a=%d:b=%d", m,n,a,b);
   test();
}
 
void test()
{
   printf("\n\nAll variables are accessed from" \
   " test function");
   printf("\nvalues: m=%d:n=%d:a=%d:b=%d", m,n,a,b);
}

Output

All variables are accessed from main function
values: m=21:n=42:a=55:b=110

All variables are accessed from test function
values: m=21:n=42:a=55:b=110

Local Variables

Local variables are declared within a function definition and thus could only be accessed from anywhere in that function in which it is declared. Thus, local variables have their scope local to the function in which they are declared and are unknown to other functions outside their own function.

Local variables are defined and initialized every time a function containing them is called and as soon as the compiler exits from that function, the local variable is destroyed.

Example program for local variable in C:

  • The scope of local variables will be within the function only.
  • These variables are declared within the function and can’t be accessed outside the function.
  • In the below example, m and n variables are having scope within the main function only. These are not visible to test function.
  • Like wise, a and b variables are having scope within the test function only. These are not visible to main function.

Program

#include
void test();
 
int main()
{
   int m = 21, n = 42;
        // m, n are local variables of main function
        /*m and n variables are having scope
        within this main function only.
        These are not visible to test funtion.*/
        /* If you try to access a and b in this function,
        you will get 'a' undeclared and 'b' undeclared error */
   printf("\nvalues : m = %d and n = %d", m, n);
   test();
}
 
void test()
{
   int a = 55, b = 110;
        // a, b are local variables of test function
        /*a and b variables are having scope
        within this test function only.
        These are not visible to main function.*/
        /* If you try to access m and n in this function,
        you will get 'm' undeclared and 'n' undeclared
        error */
   printf("\nvalues : a = %d and b = %d", a, b);
}

Output

 
values : m = 21 and n = 42
values : a = 55 and b = 110

Program

/*Program to illustrate c global variables*/

#include
int a=5; // global variables
void main()
{
    ++a;
    printf("%d \n",a);
    increment();
    return 0;
}
void increment()
{
    ++a;
    printf("%d \n",a);
}

Output

 error:[Error] C:\Users\sample c program\program.c:15: 'a' undeclared (first use in this function)

In the above program, an error will occur as in the function increment() we are trying to access variable a which is not known to it. It should be noted that the variable a is local to the function main().