Solved

#if or #ifdef  with two conditions

Posted on 2008-10-22
7
2,675 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
stack 22 172
voltage to force translation ? 8 99
using pointers to pointers to write to a two dimensional array 16 94
C++ :Change value from  DisableCMD registry 4 66
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

831 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