Friend asked me for help in C++. I do not use C++ only C#(WP,WPF,WinForms) and Java(Android).
The part of the task he has is when the macro STAR is defined he should draw a christmass tree with * (stars) when the macro EQ is defined he suppose to draw it with = (assign operator). If both or none is defined compilation has failed. Drawing christmass tree is easy task in any language but I have problems with those preprocessor macros.
#define STAR *
#define EQ =
#if !(defined(STAR) ^ defined(EQ))
FAIL TO COMPILE?
#endif
How to check then in code which macro is defined and assign its' value to the char character;
?
The directive ' #error ' causes the preprocessor to report a fatal error. The tokens forming the rest of the line following ' #error ' are used as the error message. The directive ' #warning ' is like ' #error ', but causes the preprocessor to issue a warning and continue preprocessing.
#ifdef: It returns true if a certain macro is defined. #ifndef: It returns true if a certain macro is not defined. #if, #elif, #else, and #endif: It tests the program using a certain condition; these directives can be nested too.
Inside of a C source file, you can use the #ifdef macro to check if a macro is defined.
Macro: a word defined by the #define preprocessor directive that evaluates to some other expression. Preprocessor directive: a special #-keyword, recognized by the preprocessor. Show activity on this post. preprocessor modifies the source file before handing it over the compiler.
You want the #error pre-processor directive
There is a preprocessor directive for that:
#error "One and only one of STAR or EQ should be defined"
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