Topic : Compiler does not generate warning/error for duplicate bit variables

Forum : ST7/STM8

Original Post
Post Information Post
January 18, 2010 - 8:24pm
Guest

char var1;
char var1; // The compiler flags this
bit var2;
bit var2; // The compiler doesn't flag this

Is this behavior intentional?

Replies
Post Information Post
+1
0
-1
January 19, 2010 - 8:58am
Raisonance Support Team

Hi Jacob,

Thanks for your remark.

In C, you can declare a variable several times, if the identifier has some linkage.
This means that if "char var1" is a global variable, you can declare it several times -- but not if it is a local variable.

So:

char var1;
char var1;
bit var2;
bit var2;

Is perfectly legal C. (I am surprised about your "The compiler flags this" comment. I verified that our compiler does NOT flag it).

However, it is allowed only for declarations, not for definitions.

char var1 = 2;  // First definition of var1
char var1 = 2;  // Second definition of var1 => ERROR

And also, you cannot have several declarations for local variables in a function.

So in brief, the code you submitted is perfectly legal C (excepted that the "bit" type is an extension to the standard), and the compiler has the appropriate behavior.

Let us know if you have an issue with this.
Regards,
Bruno

+1
0
-1
January 27, 2010 - 11:12pm
Guest

Thanks for that information; I did not realize that was legal syntax. Reviewing my actual code, I found that I'd made a typographical error, so as you suggested, they did not have the same linkage.