Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the preprocessor macro "#define TRUE FALSE'" valid? [closed]

Is the following define statement valid in C?

#define TRUE FALSE
like image 737
TheKojuEffect Avatar asked Sep 19 '13 12:09

TheKojuEffect


People also ask

Is macro and preprocessor same?

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.

Is macro a preprocessor directive?

There are 4 Main Types of Preprocessor Directives: Macros. File Inclusion. Conditional Compilation. Other directives.

What are the macros in C?

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.

What is the use of preprocessor macro?

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.


2 Answers

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.

like image 197
Some programmer dude Avatar answered Sep 20 '22 04:09

Some programmer dude


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
like image 36
dhein Avatar answered Sep 22 '22 04:09

dhein