Skip to content
Home » How to call a Global variable in to a function in C

How to call a Global variable in to a function in C

Learn about C Program to call a Global variable in to a function in C in the below code example.
Also refer the comments in the code snippet to get a detailed view about what’s actually happening.

C Program to call a Global variable in to a function in C:

To call global variable in a function after local declaration it must be recalled .for recalling global variable it should be defined using extern keyword. After defining it, global variable is used inside the particular loop of declaring extern variable.

#include<stdio.h>
int a=4;//Global Variable
void cal()
{
        int a=2;//Local Variable
        a+=a;
        printf("VALUE IS (local variable): %d\n",a);
        {
            extern int a;//recalling global variable
            a=a+a;                        
            printf("VALUE IS (global variable): %d\n",a);  
        }
}

void main()
{ 
    int i;
    for(i=1;i<4;i++)
    {
        cal();
    }
}

VALUE IS (local variable): 4
VALUE IS (global variable): 8
VALUE IS (local variable): 4
VALUE IS (global variable): 16
VALUE IS (local variable): 4
VALUE IS (global variable): 32

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Also Read:

using static int as counter