#include <iostream>
#define true false
#define false true
int main() {
std::cout << false << true;
}
Why does it output “01”?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
As Jerry Coffin notes, you cannot define a macro with a name that is a keyword.
However, we could consider another, similar example, with well-defined behavior and the same result. Consider:
When you use
FALSE, it is identified as the macroFALSEand is replaced by that macro’s replacement list, which is the single token,TRUE. That replacement is then rescanned for further macros to replace.The
TRUEin the replacement is then identified as a macro and is replaced by its replacement list, which is the single tokenFALSE. That replacement is again rescanned.If we continued on rescanning and replacing, we’d end up in an infinite loop, so the C (and C++) preprocessing specifications state that macro replacement never recurses within a replacement list.
Since replacement of
FALSEin this final replacement list would result in recursion, macro replacement stops and we are left withFALSE, which is the name of anintwith a value of0.