LOCAL AND GLOBAL VARIABLE IN C++

 VARIABLE SCOPE

A variable's scope is defined by where the variable is initialized or created.

or A scope of a variable is region in  the program where the existence of that variable is valid.

Based on Scope, Variables can be classified into two types.

LOCAL VARIABLE :  Local variable are declared inside the braces of any function and can be                                                                   assessed  only from there.//our program give it priority.

GLOBAL VARIABLE : Global variable are declared outside of any function and can be assessed from                                               anywhere.


DATA TYPE :  Data type define the type of data that a variable can hold.


tut3.c

//local and global variable in c++.
#include <iostream>
using namespace std;

   int main()
 {
 int a = 3 , c = 1 ;

 float b = 7.3 ;

 char charcter = 'M' ;

 double d = 3.7777 ;
 
 bool e  = true, f = false ;

 //cout << a << b << c << d;

cout<<"The value of a is :\n"<< a;
 
  cout<<"\nThe value of b is :\n"<<b;

cout<<"\nThe value of c is :\n" <<c ;

  cout<<"\nThe value of d is:\n"<< d ;

cout<<"\nThe value of charcter is :\n" << charcter;

  cout<<"\nThe value of e is:\n"<<e;

cout<<"\nThe value of f is:\n"<<f;
return 0;
}

//OUTPUT
The value of a is : 3 The value of b is : 7.3 The value of c is : 1 The value of d is: 3.7777 The value of charcter is : M The value of e is: 1 The value of f is: 0


tut3-1.c

//local and global variable in c++.
#include <iostream>
using namespace std;
 
int glo = 9; /* to yhn pe jo
                glo hai wo ak
globle variable hai..
or agar hm binna function ke
ander dusra koi glo variable
na banay or tb cout kre glo
ko tb ans 9 aayga */

 int main(){

 int glo = 77 ; /* ab hamne
                  main()
function ke ander glo ak
variable liya hai to Functions
ke ander jo bhi variable lete
hain wo local variable ban
jaata hai or ab agar hm cout
likhe to ans 77 aayga kunki
hamara program local variable
ko precedence deta hai.. */

 glo = 6 ; /* esa krne se
            variable update
hota hai or ab agar cout
krenge to ans 6 aayga..*/

 cout<<glo ;

return 0;
}
//OUTPUT
6


tut3-2.c

// local and global variable in c++.

#include <iostream>
using namespace std;

int glo = 9; /* it is Global
                Variable */

void sum()
{

  cout << glo;
}

int main()
{

  int glo = 77; /*it is local
                 Variable
becouse writen inside main()
Function So it has Presedense
 */

  glo = 6; /*we update our
            variable*/

  sum(); /* sum() is a
           function which
can execute by this type */

  cout << glo;

  return 0;
}
//OUTPUT
96
















Comments