Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make compilation fail if both or none preprocessor macro is defined

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;?

like image 957
Yoda Avatar asked Mar 17 '14 13:03

Yoda


People also ask

Which can generate preprocessor error?

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.

Which of the following directive processor will return true if this macro is defined?

#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.

How do I know if a macro is defined?

Inside of a C source file, you can use the #ifdef macro to check if a macro is defined.

What is the difference between macro and preprocessor?

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.


2 Answers

You want the #error pre-processor directive

like image 114
Sean Avatar answered Sep 23 '22 11:09

Sean


There is a preprocessor directive for that:

#error "One and only one of STAR or EQ should be defined"
like image 20
Shahbaz Avatar answered Sep 19 '22 11:09

Shahbaz