Reputation: 2481
What kind of variable assignment syntax is this? Please explain why this code works the way it does and how? It seems that the variables are assigned without the =
operator. Any subsequent tests reveal that pi_num
returns 3.14
. Why?
#define SPECIAL_NUMBER 3.14
cout << "Special number is " << SPECIAL_NUMBER << endl;
#define SPECIAL_VARIABLE pi_num
float SPECIAL_VARIABLE = SPECIAL_NUMBER;
cout << "Pi: " << pi_num << endl;
Upvotes: 0
Views: 67
Reputation: 36597
What's happened is that you have obscured what is going on using macros.
Bear in mind that thje preprocessor does text substitution, and replaces macros with their expansion. In your code every usage of SPECIAL_NUMBER
will be replaced by 3.14
and every usage of SPECIAL_VARIABLE
by pi_num
BEFORE the code is compiled.
So the compiler sees your code as
cout << "Special number is " << 3.14 << endl;
float pi_num = 3.14;
cout << "Pi: " << pi_num << endl;
I'll leave the debate alone over whether float pi_num = 3.14
is an assignment or an initialisation (OP unlikely to understand the distinction).
I assume you're aware that the mathematical quantity known as pi (greek letter) is only approximately equal to 3.14
.
Upvotes: 1
Reputation: 92261
After preprocessing this would look like
cout << "Special number is " << 3.14 << endl;
float pi_num = 3.14;
cout << "Pi: " << pi_num << endl;
No magic involved.
Upvotes: 1