Wednesday, April 2, 2014

[C++]extern

You will use extern keyword to declare a variable at any place. Though you can declare a variable multiple times in your C++ program, but it can be defined only once in a file, a function or a block of code.

#include <iostream>
using namespace std;

// Variable declaration:
extern int a, b;
extern int c;
extern float f;
  
int main ()
{
  // Variable definition:
  int a, b;
  int c;
  float f;
 
  // actual initialization
  a = 10;
  b = 20;
  c = a + b;
 
  cout << c << endl ;

  f = 70.0/3.0;
  cout << f << endl ;
 
  return 0;
}

No comments:

Post a Comment