I have read that I am not able to include preprocessor directives inside my function. But when I implement it, it works. Here are my code:
#include <iostream>
#define MY_CONSTANT 41
void myFunction(int x) {
#if x == 42
std::cout << "Value is 42." << " "<<x<<std::endl;
#else
std::cout << "Value is not 42." <<" "<<x<< std::endl;
#endif
}
int main() {
myFunction(42);
return 0;
}
'The output was not like what I expected. It is: Value is not 42, 42. Could you explain me what really happened here. Why the directive come to the else statement.'
#if adhfsgfhsfglkjnand get the same result. For preprocessor yourxis simply not defined, so a whole condition evaluates to false. This could help you: stackoverflow.com/a/1643874/4165552x, sox == 42is false. As such, by the time the code gets to the compiler, the function looks likevoid myFunction(int x) { std::cout << "Value is not 42." <<" "<<x<< std::endl; }