Solved

#if or #ifdef  with two conditions

Posted on 2008-10-22
7
2,579 Views
Last Modified: 2012-05-05
C programming...

I have two conditions for which I need to test in the same #if or  #ifdef (e.g. NTCONSOLE and EXPANDED). What is the correct format? (I've seen several when I look through eisting code)

1) #if NTCONSOLE  && EXPANDED
2) #if defined(NTCONSOLE) & defined(EXPANED)

Are these two syntaxes  equivalent, and if not, which is preferred?
0
Comment
Question by:Stephen Kairys
  • 3
  • 2
  • 2
7 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 22778986
#ifdef checks whether the macro has been defined.
#if checks the value of the macro.

The below code will only print "ifdef"
#include <stdio.h>
 

#define TEST 0
 

int main(void) {

#ifdef TEST

  printf("ifdef\n");

#endif

#if TEST

  printf("if\n");

#endif

  return 0;

}

Open in new window

0
 
LVL 5

Accepted Solution

by:
zmo earned 125 total points
ID: 22779213
as infinity08 told, #if alone is not valid.

so the only options you have are :

1)
#ifdef NTCONSOLE
#ifdef EXPANDED

#endif
#endif

or

2)
#if defined(NTCONSOLE) && defined(EXPANED)

#endif

by the way, you used '&' in your option #2, and be careful, that one makes a logical binary operation and not a binary comparison. ;)
0
 
LVL 5

Expert Comment

by:zmo
ID: 22779221
and the best one is the one you prefer, but for consistency, choose one and keep your choice for all your project.
0
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 
LVL 53

Expert Comment

by:Infinity08
ID: 22779302
>> #if alone is not valid.

I did not say that. It IS valid - it just does something different from #ifdef
0
 
LVL 4

Author Comment

by:Stephen Kairys
ID: 22779369

 
>> #if defined(NTCONSOLE) && defined(EXPANED)

by the way, you used '&' in your option #2, and be careful, that one makes a logical binary operation and not a binary comparison. ;) <<

Zmo, I  am going with the above sol'n. Thank you. However, what' od is that using the syntax with the single & (that is #if defined(NTCONSOLE) & defined(EXPANDED)
seemed to work in my test program. What's the deal with that?

Thanks again.
-Steve
0
 
LVL 5

Expert Comment

by:zmo
ID: 22784160
Infinity08:
>> #if alone is not valid.
> I did not say that. It IS valid - it just does something different from #ifdef

of course, I meant what you meant ;)

> #if defined(NTCONSOLE) & defined(EXPANDED) seemed to work in my test program. What's the deal with that?

<http://gcc.gnu.org/onlinedocs/cpp/Defined.html>
''defined name and defined (name) are both expressions whose value is 1 if name is defined as a macro at the current point in the program, and 0 otherwise.''

i.e. #if 1 & 1 will always be true, and #if 1 & 0 will always be false. Then you'd ask me why shall I care whether I use '&' or '&&' ?

<http://gcc.gnu.org/onlinedocs/cpp/If.html>
''Arithmetic operators for addition, subtraction, multiplication, division, bitwise operations, shifts, comparisons, and logical operations (&& and ||). The latter two obey the usual short-circuiting rules of standard C.''

ie if you use '&' you won't have lazy evaluation (or short-circuiting rules) though with && you will.
0
 
LVL 4

Author Closing Comment

by:Stephen Kairys
ID: 31508857
I used option #2:
#if defined(NTCONSOLE) && defined(EXPANED)

Thanks.
0

Featured Post

Free book by J.Peter Bruzzese, Microsoft MVP

Are you using Office 365? Trying to set up email signatures but you’re struggling with transport rules and connectors? Let renowned Microsoft MVP J.Peter Bruzzese show you how in this exclusive e-book on Office 365 email signatures. Better yet, it’s free!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now