Global Variable in C/C++

Declaring and Initializing Global Variable in C/C++ Language

Before declaring Global variable we must be know that:
1- The scope of global variables will be throughout the program.
2- These variables can be accessed from anywhere in the program.
3- This variable is defined outside the main function. So that, this variable is visible to main function and all other sub functions.

Now we take an example
In this example we make two function one is main ( ) and other is test ( ) and declare two variable as global variable named marks and t_marks.


#include<stdio.h>
void test() /*( in this line we declear and intialize the Global Variable marks and t_marks)*/
int marks = 320, t_marks = 500; 
int main()
{
printf("All variables are accessed from main function");
printf("\n marks=%d /n t_marks=%d:", marks,t_marks);
test( );
}

void test( )
{
printf("\n All variables are accessed from in test function") ;
printf("\n marks=%d /n t_marks=%d:", marks,t_marks);
}

in above example its display same value in both function .