Solved

#if or #ifdef  with two conditions

Posted on 2008-10-22
7
2,536 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
Comment Utility
#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
Comment Utility
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
Comment Utility
and the best one is the one you prefer, but for consistency, choose one and keep your choice for all your project.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
>> #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
Comment Utility

 
>> #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
Comment Utility
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
Comment Utility
I used option #2:
#if defined(NTCONSOLE) && defined(EXPANED)

Thanks.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
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 how to use strings and some functions related to them in the C programming language.

771 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

14 Experts available now in Live!

Get 1:1 Help Now