Redeclaration of local variables not caught

Posted By: Uhrwerk

Redeclaration of local variables not caught - 02/11/13 02:41

Hello jcl,

the following code should be rejected with an error message but instead compiles and executes:
Code:
#include <acknex.h>

void main()
{
	int i = 0;
	int i = 0;
}



Best regards,
Uhrwerk
Posted By: jcl

Re: Redeclaration of local variables not caught - 02/11/13 09:13

Redeclaring variables is allowed in lite-C.
Posted By: Uhrwerk

Re: Redeclaration of local variables not caught - 02/11/13 15:49

Ok. I didn't find this case specified in the C Reference Manual. So I tried some other compilers and they all gave an error message similar to "error: redeclaration of 'i'". What's the purpose of redeclaring local variables?
Posted By: Rackscha

Re: Redeclaration of local variables not caught - 02/11/13 17:25

oh, redeclaring is allowerd and mostly results in this spaghetie source:

Code:
for(int i = 0; i < 10; i++)
{
  for(int i = 0; i < 100; i++)
  {
    dosomethingwithint(i);
  }
}



I hate nested loops with redeclared vars ARGH -.-
Posted By: Uhrwerk

Re: Redeclaration of local variables not caught - 02/11/13 17:28

That's a different example. In your code snippet two variables with identical name are declared as well, but they have got a different scope.
Posted By: Rackscha

Re: Redeclaration of local variables not caught - 02/11/13 19:35

@Uhrwerk: yes they have different scopse, nontheless:
Redeclaring the same identifier over and over again just starts to confuse other people when reading the source tongue
Posted By: Uhrwerk

Re: Redeclaration of local variables not caught - 02/11/13 20:37

I absolutely agree. Nevertheless this is not related to the original topic.
Posted By: jcl

Re: Redeclaration of local variables not caught - 02/12/13 10:52

The templates make heavy use of redeclaring variables.
© 2024 lite-C Forums