Multiple #define with same name
-
What will happen if there are mutiple #define with same name? For eg: in one file there is, #define MY_NAME 1 and in another file, #define MY_NAME 3 I have seen that the compiler gives a warning for this. But what will be the value of "MY_NAME" when it is used? is it 1 or 3?
-
What will happen if there are mutiple #define with same name? For eg: in one file there is, #define MY_NAME 1 and in another file, #define MY_NAME 3 I have seen that the compiler gives a warning for this. But what will be the value of "MY_NAME" when it is used? is it 1 or 3?
-
What will happen if there are mutiple #define with same name? For eg: in one file there is, #define MY_NAME 1 and in another file, #define MY_NAME 3 I have seen that the compiler gives a warning for this. But what will be the value of "MY_NAME" when it is used? is it 1 or 3?
First I guess, this is bad design, why ? two definition, you should have ideally one define ( in header file) and include that file where the value being used. On other hand technically if two files are unrelated files, each should have there respective values. If one file is calling function in another or they are two separate header files, included in a single cpp file, you compiler should give error,
-
What will happen if there are mutiple #define with same name? For eg: in one file there is, #define MY_NAME 1 and in another file, #define MY_NAME 3 I have seen that the compiler gives a warning for this. But what will be the value of "MY_NAME" when it is used? is it 1 or 3?
The compiler gives a warning in this case because even though this practice is totally acceptable, it may indicate some type of oversight. In cases like this, the compiler's warning can be ignored if it is intentional; otherwise, it should be corrected. As for why this is acceptable by the compiler, it is because it does not violate any rules of the language. The value of the defined symbol when it is referenced depends on the order of the two
#define
s that was read by the compiler when it is in the preprocessing stage. The preprocessor will use the value of the one that appeared cloest to the reference to substitute the symbol. By compile time this substitution is already done. I would avoid this practice since it is error prone. -
What will happen if there are mutiple #define with same name? For eg: in one file there is, #define MY_NAME 1 and in another file, #define MY_NAME 3 I have seen that the compiler gives a warning for this. But what will be the value of "MY_NAME" when it is used? is it 1 or 3?
If the compiler gives a warning, fix it. either rename one of the offending define or depending on what you want to do, you can try something like this to re assign value to a #defined value:
#define MY_NAME 1
#ifdef MY_NAME
#undef MY_NAME
#define MY_NAME 3
#endifWatched code never compiles.
-
If the compiler gives a warning, fix it. either rename one of the offending define or depending on what you want to do, you can try something like this to re assign value to a #defined value:
#define MY_NAME 1
#ifdef MY_NAME
#undef MY_NAME
#define MY_NAME 3
#endifWatched code never compiles.
Hi, there is no need to test (see MSDN[^]), you can reduce it to:
#undef MY_NAME
#define MY_NAME 3as #undef never complains about undefined symvols. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.
-
Hi, there is no need to test (see MSDN[^]), you can reduce it to:
#undef MY_NAME
#define MY_NAME 3as #undef never complains about undefined symvols. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.
Yep you're right. :)
Watched code never compiles.
-
What will happen if there are mutiple #define with same name? For eg: in one file there is, #define MY_NAME 1 and in another file, #define MY_NAME 3 I have seen that the compiler gives a warning for this. But what will be the value of "MY_NAME" when it is used? is it 1 or 3?
With #define constants, you should try to surround the value with paranthesis. It also is safer to check if there is a name conflict and throw an error if there is. You do not want to use the wrong constant in another file! This could happen with the use of #undef #ifdef MY_NAME #if MY_NAME!=(3) #error Conflicting definitions of MY_NAME #endif #else #define MY_NAME (3) #endif
-
What will happen if there are mutiple #define with same name? For eg: in one file there is, #define MY_NAME 1 and in another file, #define MY_NAME 3 I have seen that the compiler gives a warning for this. But what will be the value of "MY_NAME" when it is used? is it 1 or 3?
dipuks wrote:
But what will be the value of "MY_NAME" when it is used? is it 1 or 3?
You would have spent less time just trying it yourself. :rolleyes:
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius