1

In principle, a variable defined outside any function (that is, global, namespace, and class static variables) is initialized before main() is invoked. Such nonlocal variables in a translation unit are initialized in their declaration order

Above are the lines from the class notes given by my lecturer.

#include <iostream>

using namespace std;
int a=99;
int main(int argc, char *argv[]) 
{
  cout<<a<<endl;
  cout<<b<<endl;
  return 0;
}
int b=100;

There is an error while I run this. Isn't it true that b assigned to 100 before main() is called?

apple apple
  • 10,292
  • 2
  • 16
  • 36
tez
  • 4,990
  • 12
  • 47
  • 67

5 Answers5

5

The problem here is not initialisation order: b is indeed initialised before main starts running.

The problem is the "visibility" of b. At the point where main is being compiled, there is no b.

You can fix it by either moving the definition/initialisation of b to before main:

#include <iostream>

using namespace std;
int a = 99;
int b = 100;
int main (int argc, char *argv[]) {
    cout << a << '\n';
    cout << b << '\n';
    return 0;
}

or simply indicate that b exists:

#include <iostream>

using namespace std;
int a = 99;
extern int b;
int main (int argc, char *argv[]) {
    cout << a << '\n';
    cout << b << '\n';
    return 0;
}
int b = 100;

Neither of those two solutions change when b is created or initialised at run-time, they simply make b available within main.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
3

Your lecturer is wrong; global variables are initialised in order of definition, not declaration.

For example,

#include <iostream>
struct S { S(const char *s) { std::cout << s << '\n'; } };
extern S a;    // declaration
extern S b;    // declaration
int main() { }
S b("b");      // definition
S a("a");      // definition

will print

b
a

The code you posted doesn't work because b is not even declared at the point of use. A declaration (for example, extern int b), is required because C++ (like C) was originally designed as a one-pass compiler.

ecatmur
  • 152,476
  • 27
  • 293
  • 366
2

The problem is here: cout<<b<<endl;

You can't access the variable before it's declaration.

Andrew
  • 24,218
  • 13
  • 61
  • 90
2

Read carefully.

A variable defined outside any function (B) is initialized before main is invoked (not compiled).

To be compiled correctly B should be defined(and declared) before it's first use (and that's your error: B is used before been declared anywhere).

Heisenbug
  • 38,762
  • 28
  • 132
  • 190
0

The problem is the statement b=100;: you can't place a statement in a global scope (except for variable initialization).

If this line remains as is, the code won't compile regardless of b declarations/definitions or the use of b within main or anywhere.

Without this line, the code is correct and works, with b value equals 0, since uninitialized global variables are initialized to 0 (or the line b=100; could be moved into any function scope).

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Yossis
  • 1