Solved

#if or #ifdef  with two conditions

Posted on 2008-10-22
7
2,723 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Test against App 49 138
Intel debugger, idb, only seeing .c and .h, but no .f90 1 474
xamarin c# deserialize Json containing nested object 2 149
Computer slow / BSOD 10 70
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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

726 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