Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
629 views
in Technique[技术] by (71.8m points)

c - Is #if defined MACRO equivalent to #ifdef MACRO?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
#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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...