declaration

for a global variable i can declare in this way:
#include<iostream>
int x=5;
int main(){..}
but this gives an error..why??
#include<iostream>
int x;
x=5; //error in this line
int main{....}
error stated is:
expected constructor, destructor, or type conversion before '=' token
why??
Last edited on
you may not initialise the value of your variable in the global scope unless it's a constant.

1
2
3
4
5
int x;
int main()
{
    x = 5;
}


or

1
2
3
4
const int x=5;
int main()
{
}
you can declare non constant global variables. but i would discourage you from using global variables because 50% of the errors you will get when you get to larger programs will be from global variables because they become so hard to coordinate how they are suppose to be used. later in your programming studies you will learn about classes. that makes this whole thing easier.
Last edited on
my querry was that why for a global variable:
int x=5; is right but declaring
int x;
x=5; is an error
expected constructor, destructor, or type conversion before '=' token ..
and i know about classes.
closed account (zb0S216C)
Any statements that modify an object must be within a function. Only declarations can appear within the global namespace.

Wazzak
Because simply executing commands requires a sequential part of code. Sequential stuff are defined only in scopes of functions.

When you define anything in the global scope, according to the C++ standard, the behaviour and the order of definitions is UNDEFINED... so NEVER DO IT!

If you want something to be available on the global scope, use static variables.
TheDestroyer wrote:
When you define anything in the global scope, according to the C++ standard, the behaviour and the order of definitions is UNDEFINED... so NEVER DO IT!

That is not true. The initialization order of variables defined in the same translation unit is initialized in the order they are defined.
According to the C++ standard, it's undefined. You can be a smart guy and "find out" how it's done and follow it, but you can't guarantee that all compilers will have the same behaviour. I read that in the book:

Professional C++ 2005 by Solter, Kleper.
The book is probably talking about global variables defined in different files.
Last edited on
Does it really make a difference? are you really gonna use a program with a single file unless it's some crappy test? come on! we're talking concepts here!!!
How many files a project has doesn't have anything to do with the order of initialization. Besides, the order is completely irrelevant unless variables depend on each other. There are much more important reasons not to use global variables than that.
Last edited on
Example 1:
1
2
3
4
5
6
7
#include <iostream>
int x = 9;
int y = 2 * x;
int main()
{
	std::cout << y << std::endl;
}

This program is totally defined. First all the global variables will be initialized. First x will be initialized to 9 because it is defined first. Then y is initialized and get the value 18. The main function doesn't start until all the global variables has been initialized, so we can be sure the program will print 18.


Example 2:
1
2
3
4
5
6
7
#include <iostream>
extern int x;
int y = 2 * x;
int main()
{
	std::cout << y << std::endl;
}

 
int x = 9;

Here x is defined in another file so we don't know which of x and y that is initialized first. We don't know what value the program will print.
With all due respect, why is it so hard for you to understand that IT IS NOT IN THE STANDARD... compilers can do that for you because they're nice neat programs!!!!

The order with which the variables are defined in the global-scope IS UNDEFINED!!!!

Since I'm a physicist, I'm gonna tell you rule in physics:

"100 examples don't prove a rule, but a single wrong example denies a theory."

And I mentioned previously that trivial examples are not included... didn't I??? (so don't tell me the single file stuff)

And like Athar said, there are dozens of other reasons because of which one has to avoid global variables... what are you trying to prove exactly?

And this part is from the book, to make you live in peace:

However, C++ provides no specifications or guarantees about the initialization ordering of nonlocal
variables in different source files. If you have a global variable x in one source file and a global variable y
in another, you have no way of knowing which will be initialized first. Normally, this lack of specification
isn’t cause for concern. However, it can be problematic if one global or static variable depends on
another. Recall that initialization of objects implies running their constructors. The constructor of one
global object might access another global object, assuming that it is already constructed. If these two
global objects are declared in two different source files, you cannot count on one being constructed
before the other.

Initialization order of nonlocal variables in different source files is undefined.


Peace :-)
I don't recommend globals. I know there are many reasons not to use globals. I just don't like false information being used as an argument.

I have no problems with what your book says. It talks about globals in different files. As long as you only use globals defined earlier in the same file to initialize a global variable there is no problem. The standard says so (§3.6.2).
Topic archived. No new replies allowed.