This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Global Variable.

In my project I have two files. as follows

File A
int x;
main()
{
x=2;
}

File B

extern int x;
{
x=4;
}

When I compile both files independatly, there is no error. When I link both modules, there is no error. But when I execute this program, I am unable to assign x = 4. Always the value of x remains 2. But in file A I can assign any value to x. Why I can not assign any value to x in File B? I am trying this with Keil compiler.

Parents
  • the Compiler would perform this optimisation

    Not on a global variable like this it wouldn't. Or if it did, that'd be a bug.

    The compiler simply doesn't have the information needed to justify eliminating any object of external linkage --- it doesn't know what other translation units might be doing with it.

Reply
  • the Compiler would perform this optimisation

    Not on a global variable like this it wouldn't. Or if it did, that'd be a bug.

    The compiler simply doesn't have the information needed to justify eliminating any object of external linkage --- it doesn't know what other translation units might be doing with it.

Children