Index

C & C++ Programming - Static & Global Variables

#ifndef STATIC_GLOBAL_TESTING01_H
#define STATIC_GLOBAL_TESTING01_H

void Counter1();

#endif

#include "stdafx.h"

int g_nCount = 1;

/**************************************************************************
1. Varible defined as global or static retains the previous value.
2. Varible can be defined as either static or global.
3. Static varible can't be used as extern.
4. Static varible can't be passed as reference or pointer
**************************************************************************/

void Counter()
{
 g_nCount++;

 cout << "Count in File 0 = " << g_nCount << endl;
}

#include "stdafx.h"

extern int g_nCount;

void Counter1()
{
 g_nCount++;

cout << "Count in File 1 = " << g_nCount << endl;
}

#include "stdafx.h"
#include "Static_Global_Testing01.h"

extern void Counter();

int _tmain(int argc, _TCHAR* argv[])
{
 for(int nCnt = 0; nCnt < 10; nCnt++)
 {
  Counter();
  Counter1();
 }

 return 0;
}


Index