Global Variables in C : A Step-by-Step Guide


In C programming, a global variable is a variable that is declared outside of any function or block of code. Global variables are accessible from anywhere in the program, including within functions and blocks of code, and their values can be modified and accessed by any part of the program.

The basic syntax for declaring a global variable in C is as follows:

           data_type variable_name ;

Here, the data_type specifies the data type of the variable, and the variable_name is the name of the variable. The variable is typically declared outside of any function, at the beginning of the program, before the main function.

Here is an example of a global variable being used in a program:

Source Code

//Global Variable
 
#include<stdio.h>
 
void display();
 
int a=10;
 
int main()
{
    printf("\n Value of A : %d",a);
    display();
    return 0;
}
 
void display()
{
    a++;
    printf("\n Value of A : %d",a);
}
 
To download raw file Click Here

Output

 Value of A : 10
 Value of A : 11

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C