News:

Download Pelles C here: http://www.smorgasbordet.com/pellesc/

Main Menu

redeclaration

Started by dsula, April 26, 2006, 11:42:13 PM

Previous topic - Next topic

dsula

Pelle,

The compiler doesn't catch redeclaration of global vars.
e.g.

int a, a;
int main(void) {...}

It catches redeclaration of auto vars, though.

ds

Pelle

Just a quick test: one Microsoft, and one GNU, compiler seems perfectly happy with this - not even a warning. I will see if the C standard says anything about it... (not sure it does).

Pelle
/Pelle

frankie

I think it could be consistent with the sharing of global variables.
If I well remember M$ C compiler (the old DOS version at least) automatically allocate a global variable if are present one or more external declarations of the same variable (i.e. "#external int a"), also if there is no allocation (i.e. "int a") in any module.
Of course defining the same variable on the same line and in the same module seems odd, but sintactically correct for the language grammar.
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

dsula

My old DOS Borland C v3.1 complains about it. Well anyways. I see no reason why there should not be at least a warning. It took me a long time to debug till I finally realized I had accidently declared 2 variables with the same name. Are there any reasons ?

Pelle

Seems to be OK with the C standard. No bug, no warning.

(If you really need to catch this - why not intialize the variables? "int a = 0, a = 0;" is a different matter, and will cause an error...)

Pelle
/Pelle

frankie

I don't see practical problems if the variable is the same, if you mean that the compiler accept the redeclaration and create different variables then we have a big problem.
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

Pelle

/Pelle