NO

Author Topic: redeclaration  (Read 4438 times)

dsula

  • Guest
redeclaration
« on: April 26, 2006, 11:42:13 PM »
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

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
redeclaration
« Reply #1 on: April 27, 2006, 02:47:47 PM »
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

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
redeclaration
« Reply #2 on: April 27, 2006, 03:17:41 PM »
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

  • Guest
redeclaration
« Reply #3 on: April 27, 2006, 09:26:52 PM »
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 ?

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
redeclaration
« Reply #4 on: April 27, 2006, 10:51:58 PM »
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

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
redeclaration
« Reply #5 on: April 28, 2006, 04:47:39 PM »
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

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
redeclaration
« Reply #6 on: April 29, 2006, 05:26:19 PM »
It's the same...
/Pelle