Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does order matter when #defines are using other #defines?

According to the answer to this question, the following code is legit:

#define three 3 #define nine three*3  int main() {     std::cout << nine;     return 0; } 

And sure, it compiles and runs fine. However, the answer to mentioned question also states that one should be careful about the order of such #define directives, and one that will be used in other #defines should be defined before them. But the following code:

#define nine three*3 #define three 3  int main() {     std::cout << nine;     return 0; } 

Also compiles and runs fine, and it prints "9".

Is my compiler letting me off easy, or does the order indeed not matter with #defines that use other #defines? Would the compilation fail on a more complicated project?

One thing worth mentioning is that the mentioned question speaks of C, whilst my code is in C++. Is that where the (supposed) differences in behavior come from?

like image 965
Ludwik Avatar asked Mar 22 '14 08:03

Ludwik


People also ask

How do you know when order matters in probability?

If the order doesn't matter then we have a combination, if the order do matter then we have a permutation. One could say that a permutation is an ordered combination. The number of permutations of n objects taken r at a time is determined by the following formula: P(n,r)=n!

What does it mean when order matters?

The ordering matters… A permutation is an arrangement of objects in a definite order. In mathematics, permutation is the act of arranging the members of a set into a sequence or order, or, if the set is already ordered, rearranging (reordering) its elements — a process called permuting.

Does order matter math?

From your earliest days of math you learned that the order in which you add two numbers doesn't matter: 3+5 and 5+3 give the same result.

Does order matter in combinations?

Permutations are for lists (order matters) and combinations are for groups (order doesn't matter). You know, a "combination lock" should really be called a "permutation lock". The order you put the numbers in matters.


1 Answers

three macro need be only defined before use of nine macro. You can even change three before every use of nine:

#define nine three*3 #define three 3  int main() {     std::cout << nine; //9 #undef three #define three 4     std::cout << nine; //12 #undef three     //no `three` macro defined here     int three = 2;     std::cout << nine; //three * 3 == 6     return 0; } 
like image 90
Yankes Avatar answered Oct 13 '22 01:10

Yankes