Solved

#if or #ifdef  with two conditions

Posted on 2008-10-22
7
2,637 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

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…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

803 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