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 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 .
Declaring and Initializing Local Variable in C
Declaring and Initializing Global Variable in C Language
Printf Function In C Language
Sacnf Function In C Language
for Loop in C Language
While Loop in C Language
Do While Loop in C Language
Switch Case Statement IN C
IF -Else Statement IN C
Break Statement IN C /C++
User Define Function IN C /C++
Cout/Cin Function IN C /C++
Array IN C /C++
Two Dimensional Array IN C /C++
String In IN C /C++
Pointer In IN C /C++
Const and volatile keyword IN C /C++
Class and Object in C++
Function Over Loading in C++
Character Array In C++
Constructor in C++
Operator overloading in C++
Function Templates in C++
ATM Sample Project In Simple C Language
Find Area of Shape Using Function In C++