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?

9
  • can you post the exact compilation error Commented Aug 7, 2012 at 8:52
  • 3
    You're getting confused between initialisation order and compilation order - they are not the same thing. Commented Aug 7, 2012 at 8:52
  • So,is it like,Compilation is done line by line in order.When compilation reaches cout<<b<<endl; line,it doesn't know what 'b' is.Is this the reason why there is an error here? After the program is compiled,at execution time,'b' would be initialized before main() is called.Is it so?plz clarify,though it miight be silly Commented Aug 7, 2012 at 9:03
  • 2
    @tez: yes, that's more or less correct - you need to declare b before you use it so that the compiler "sees" its declaration first. Commented Aug 7, 2012 at 9:04
  • @PaulR:Please have a look at the edited code Commented Aug 7, 2012 at 9:24

5 Answers 5

6

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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.

Comments

2

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

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

Comments

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).

Comments

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.