Demonstrate global variables - C++ Function

C++ examples for Function:Global Variable

Description

Demonstrate global variables

Demo Code

#include <iostream>
using namespace std;
#include <conio.h>        //for getch()
char ch = 'a';            //global variable ch
void getachar();/*  www . j a  va  2 s . c  om*/
void putachar();
int main()
{
   while( ch != '\r' )    //main() accesses ch
   {
      getachar();
      putachar();
   }
   cout << endl;
   return 0;
}
void getachar()           //getachar() accesses ch
{
   ch = getch();
}
void putachar()           //putachar() accesses ch
{
   cout << ch;
}

Result


Related Tutorials