Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is #if defined MACRO equivalent to #ifdef MACRO?

Tags:

I have code that I want to have two modes, debug and verbose. I define them in my header file as,

#define verbose TRUE #define debug TRUE 

In my code so far, I have just been using

#if(debug)   //code #endif 

but is it more proper to use?

#ifdef debug   // code #endif 

I read something about preprocessor macros but it didn't make sense at the time. So, I have a question: Is #if defined MACRO equivalent to #ifdef MACRO? and which one is better for enabling/disabling a particular section of code?

like image 320
msc Avatar asked Sep 02 '16 10:09

msc


People also ask

Is the meaning of is?

Is is the third person singular of the present tense of be1.

What type of word is is?

Is is what is known as a state of being verb. State of being verbs do not express any specific activity or action but instead describe existence. The most common state of being verb is to be, along with its conjugations (is, am, are, was, were, being, been). As we can see, is is a conjugation of the verb be.

Is it a verb or not?

Yes, "is" is a linking verb. Linking verbs typically link subjects to descriptions. Ex: The car is blue.

Is in use a word?

Definition of in use : being used All of the computers are currently in use.


2 Answers

#ifdef MACRO #if defined (MACRO) 

will do the exact same thing. However, the defined (MACRO) is just an expression that evaluates to 0 or 1 inside the #if, and it can be combined with other expressions. For example

#if defined (MACRO) && ! defined (MACRO2)     // Do this #else     // Do that #endif 

Try doing that with #ifdef - you can't unless your code gets really clumsy.

like image 114
gnasher729 Avatar answered Jan 12 '23 14:01

gnasher729


#if defined(MACRO) is the same as #ifdef MACRO, but is longer. On the other hand it allows to add extra conditions with || or &&.

#if MACRO is similar to #ifdef MACRO but not 100%. If MACRO is 0 then #if MACRO will be negative - it requires MACRO to be defined and not be 0. #ifdef checks only whether it is defined even without value (#define MACRO).

Now is modern to use #if and enable/disable definitions with value 1 or 0:

#define FLAG_X 1 // or 0

like image 24
i486 Avatar answered Jan 12 '23 15:01

i486