We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
I builded a small project as follows:
============================= int a; int a; // Yes the same variable declared in the same way
main() { ...// anything } =============================
this project is successfully compiled. No warnings. Why ? What is really performed: 1. Only one variable. 2. Two variable occupying different memory but for further using only one is accessible.
How to check such a possible duplicate declaration in very large project with many files included if there is no warnings for this case?
(If Variable name is the same but declarations are different [int and char for an example] error occurs as expected)
This was tested on uVision 2.30 Keil C51 7.02
Please read the instructions on how to post source code - they are immediately above the 'Message' box when you type a post.
The compiler is treating the first line as a declaration of the symbol, and the second as the defintion of it.
I have, on some occasions, mistakeingly, 'doubled' a variable declaration and found that you only get one and no error which, whether it is as it should be or not, is a non-problem for me since my variable names are structured in such a way that there will be no two variables with the same name. I'm sure Mr. Smoked Sardine can answer whether it is correct by the C standard, If he will take time to provide information instead of spewing bile, I do not care it is correct by the C standard or not.
Now, when using 'insignificant' names such as 'a' you will, of course, risk thinking you have two different variables used in two different places, but that trap you set yourself by using 'insignificant' names.
Erik
"I'm sure Mr. Smoked Sardine can answer whether it is correct by the C standard..."
It is - look up "tentative definitions".
This is the same effect that allows you to #include a header that declares a symbol in the same file that actually defines it; eg,
file.h
extern int my_global; // declarations int my_functions( int its_parameter );
file.c
#include "file.h" // declarations int my_global; // definition int my_functions( int its_parameter ) { // define body here... }
There is no problem that file.c effectively contains both the declarations and the definitions; in fact, it brings the positive benefit that the compiler can now warn you of any mismatch!
"Now, when using 'insignificant' names such as 'a' you will, of course, risk..."
Hopefully, it was purely for the sake of illustration...
PC-Lint will warn you about repeated declarations of this kind.
(Also with different messages if you change the type, or the storage class, or...)
This is a C98 or C90 language 'feature' called tentative declaration.
"This is a C98 or C90 language 'feature' called tentative declaration."
Not quite: it's called a tentative Definition.
And it certainly pre-dates C90 - it's in my K&R of 1988, based on the Draft-Proposal of the ANSI standard.
You are correct, 'C98' was a typo, also for 'Definition'; quite a long time back.
Thanks to all, after your answers and internet searching, I realized that it is really the same as Tentative Definition.
I do not care it is correct by the C standard or not.
If anyone ever allows you to work on anything safety critical let us know what it is. Please.
I'm sure Mr. Smoked Sardine can answer whether it is correct by the C standard, If he will take time to provide information instead of spewing bile,
He would shut up if you ignored him.