Is the following define statement valid in C?
#define TRUE FALSE
macros are name for some fragment of code.So wherever the name is used it get replaced by the fragment of code by the preprocessor program. There are also many predefined macros in C for example __DATE__ , __TIME__ etc.
There are 4 Main Types of Preprocessor Directives: Macros. File Inclusion. Conditional Compilation. Other directives.
Overview. Macro in C programming is known as the piece of code defined with the help of the #define directive. Macros in C are very useful at multiple places to replace the piece of code with a single value of the macro. Macros have multiple types and there are some predefined macros as well.
Macros allow you to write commonly used PL/I code in a way that hides implementation details and the data that is manipulated and exposes only the operations. In contrast with a generalized subroutine, macros allow generation of only the code that is needed for each individual use.
You have to remember that preprocessor macros are simply substituted. If you do e.g.
#define TRUE FALSE
then the processor simply replaces all places where it finds TRUE
will be replaced by whatever FALSE
is defined to.
So indeed it's a good definition. And yes it will most likely change the program workflow, possibly in very unexpected ways that may even cause undefined behavior.
Since we should expect TRUE
is already defined when FALSE
is defined too.
So in this case this would be a redefinition and be invalid.
If you stay intern the #define TRUE FALSE
would be valid to the standard, but would be invalid according to all logics I could imagine.
But a way i have already often seen was :
#define FALSE 0
#define TRUE !FALSE
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