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 #define
s 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 #define
s? 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?
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!
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.
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.
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.
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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With