Solved

ifdef

Posted on 2011-03-21
7
453 Views
Last Modified: 2012-05-11
#ifdef TEST
            // Line 1
#else
           // Line 1
#endif

What if I need multiple lines. is it possible
0
Comment
Question by:learningunix
7 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 250 total points
ID: 35184506
Yes. Everything between the #ifdef and the #endif will be treated as one unit. It is much like a regular "if" statement, but you can think of the #endif as being a substitute for the curly braces.
0
 
LVL 16

Expert Comment

by:imladris
ID: 35184509
Yup. You can have as many lines as you like. That why the #else and the #endif are there. They are used to mark the end of the conditional code. In fact, internally, C and C++ don't even "see" lines. It's just one long string of characters. To the parser, newlines, which define the end of lines, are just characters to be ignored like blanks.
0
 
LVL 86

Expert Comment

by:jkr
ID: 35184513
Absolutely - actually, that's quite common. Most header files are secured via header guards (http://faculty.cs.niu.edu/~mcmahon/CS241/c241man/node90.html), so that's not only more than one line, but merely a whole file.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:learningunix
ID: 35185640
Ok. I thought I have to use { } for multiple lines but that is not the case

This should work

#ifdef TEST
            int a = 1;
            a++;
           printf ("%d"a);            
#else
           // Line 1
           // Line 2
#endif

0
 
LVL 86

Expert Comment

by:jkr
ID: 35186117
That should definitely work. Just keep in mind that e.g. a #define statement spanning multiple lines needs a trailing backslash if you want to do something like this.
0
 
LVL 33

Expert Comment

by:sarabande
ID: 35187657
you sometimes will see in code

#if  1
   // some code

#endif

the 1 means true and it is easily changed to #if 0 if the code shouldn't be performed anymore. using constants instead of preprocessor macros has advantages for temporary code and code limited to one source cause you don't need to define/undefine a macro like TEST..

Sara
0
 

Author Closing Comment

by:learningunix
ID: 35227103
thx
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Building cUrl in Windows v7.43.0 4 28
std::atomic with custom class 6 192
computer science syllabus 3 70
I could not build boost code, 10 79
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

863 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

20 Experts available now in Live!

Get 1:1 Help Now