C++ variable scope | local and global variable in c++ | scope of variables in c++ programming language | Initializing local and global variables | My CS Tutorial
A scope is a region of the program and broadly speaking there are three places, where
variables can be declared:-
(i) Inside a function or a block which is called local variables,
(ii) In the definition of function parameters which is called formal parameters.
(iii) Outside of all functions which is called global variables.
We will learn what a function is, and it's parameter in subsequent chapters. Here let us explain
what local and global variables are.
Variables that are declared inside a function or block are local variables. They can be used
only by statements that are inside that function or block of code. Local variables are not known to functions outside their own. Following is the example using local variables:-
#include <iostream>
using namespace std;
int main ()
{
// Local variable declaration:
int a, b;
int c;
// actual initialization
a = 10;
b = 20;
c = a + b;
cout << c;
return 0;
}
Global variables are defined outside of all the functions, usually on top of the program. The global variables will hold their value throughout the life-time of your program.
A global variable can be accessed by any function. That is, a global variable is available for use throughout your entire program after its declaration. Following is the example using global and local variables:-
#include <iostream>
using namespace std;
// Global variable declaration:
int g;
int main ()
{
// Local variable declaration:
int a, b;
// actual initialization
a = 10;
b = 20;
g = a + b;
cout << g;
return 0;
}
When a local variable is defined, it is not initialized by the system, you must initialize it yourself. Global variables are initialized automatically by the system when you define them as follows:-
Data Type Initializer
int 0
char '\0'
float 0
double 0
pointer NULL
C++ variable scope | local and global variable in c++ | scope of variables in c++ programming language | My CS Tutorial
_______________________________________
Scope of variables in c++ | My CS Tutorial |
VARIABLE SCOPE
A scope is a region of the program and broadly speaking there are three places, where
variables can be declared:-
(i) Inside a function or a block which is called local variables,
(ii) In the definition of function parameters which is called formal parameters.
(iii) Outside of all functions which is called global variables.
We will learn what a function is, and it's parameter in subsequent chapters. Here let us explain
what local and global variables are.
LOCAL VARIABLES
Variables that are declared inside a function or block are local variables. They can be used
only by statements that are inside that function or block of code. Local variables are not known to functions outside their own. Following is the example using local variables:-
#include <iostream>
using namespace std;
int main ()
{
// Local variable declaration:
int a, b;
int c;
// actual initialization
a = 10;
b = 20;
c = a + b;
cout << c;
return 0;
}
GLOBAL VARIABLES
Global variables are defined outside of all the functions, usually on top of the program. The global variables will hold their value throughout the life-time of your program.
A global variable can be accessed by any function. That is, a global variable is available for use throughout your entire program after its declaration. Following is the example using global and local variables:-
#include <iostream>
using namespace std;
// Global variable declaration:
int g;
int main ()
{
// Local variable declaration:
int a, b;
// actual initialization
a = 10;
b = 20;
g = a + b;
cout << g;
return 0;
}
INITIALIZING LOCAL AND GLOBAL VARIABLES
When a local variable is defined, it is not initialized by the system, you must initialize it yourself. Global variables are initialized automatically by the system when you define them as follows:-
Data Type Initializer
int 0
char '\0'
float 0
double 0
pointer NULL
C++ variable scope | local and global variable in c++ | scope of variables in c++ programming language | My CS Tutorial
_______________________________________
Please share this post and blog link with your friends.For more programs use this blog.
If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.If you have any method of this tutorial or program or want to give any suggestion send email on hc78326@gmail.com
Created by-- HARSH CHAUHAN
No comments:
Post a Comment