By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,061 Members | 1,563 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,061 IT Pros & Developers. It's quick & easy.

Macro Conditional..........

P: n/a
Hi all,
Please can any one tell me the importance of below code...

#if 0 /*what is use of this macro conditional.since
this is every time false .*/

stmt1 ;

#else

stmt2;

#endif
Is it any reason to include above macro conditional in program ?

Sep 18 '06 #1
Share this Question
Share on Google+
12 Replies


P: n/a
code break wrote:
Hi all,
Please can any one tell me the importance of below code...

#if 0 /*what is use of this macro conditional.since
this is every time false .*/

stmt1 ;

#else

stmt2;

#endif
Is it any reason to include above macro conditional in program ?
Yes, it is an easy way to invalidate a block of code

#if 0
// All code here will be invalidated (not active)
// If there is an #else clause it will be validated
#endif

Sep 18 '06 #2

P: n/a
code break wrote:
Please can any one tell me the importance of below code...

#if 0 /*what is use of this macro conditional.since
this is every time false .*/
stmt1 ;
#else
stmt2;
#endif

Is it any reason to include above macro conditional in program ?
Someone wanted to have some compile-time options. The idea is that you
stick in some code that you want to test under specific circumstances,
perhaps for debugging purposes.

It probably isn't recommended for production code, but if some code is
being rethought for future changes you can continue to ship known
working code, while saving a new approach for future work.

I've done this sort of thing before. I consider the ifdef a sort of
bookmark for ideas I'm working on. Especially when I don't want to
commit my changes to any development path quite yet, because I'm in the
middle of "hairy-eyed" local development mode in order to hash out some
ideas.
Sep 18 '06 #3

P: n/a

code break wrote:
Hi all,
Please can any one tell me the importance of below code...

#if 0 /*what is use of this macro conditional.since
this is every time false .*/
I usually define some symbol, something like:

#define Dont_Run_This_Code_Until_Hans_Finishes_His_Module 0000000000

Sep 18 '06 #4

P: n/a
code break wrote:
Hi all,
Please can any one tell me the importance of below code...

#if 0 /*what is use of this macro conditional.since
this is every time false .*/

stmt1 ;

#else

stmt2;

#endif
Is it any reason to include above macro conditional in program ?
Yes -- because it is evaluated at compile time, it actually allows you
to prevent debugging code (for instance) from being compiled into a
program, in a way that makes it easy to turn on if you need it. (Rather
than mess with comment blocks, you can just change the 0 to 1, and have
the alternative.) Also this lets you continue to use things like syntax
highlighting and cross-reference tools, where deleting the code or
commenting it out would not.
Sep 18 '06 #5

P: n/a
code break wrote:
Hi all,
Please can any one tell me the importance of below code...

#if 0 /*what is use of this macro conditional.since
this is every time false .*/

stmt1 ;

#else

stmt2;

#endif
Is it any reason to include above macro conditional in program ?
Yes. Imagine a complex system where is one has mechanism to optionally
print some debug information when the program is running. Consider
following code

#define DEBUG 1
int main() {
int x=0;
x=10;

#if DEBUG
printf("value of x is %d\n",x);
#end if
.....
......
......
}

You can have lot of #if DEBUG in the code when you want to print or log
some useful information. A single switch i.e. changing DEBUG from 1 to
0 will turn off all printing. Setting DEBUG to 1 will enable printing.

Being a software developer myself,I have seen this type of debugging
mechanisms used in industry, mainly by the development team. It helps a
lot for quickly checking values of some variables without getting into
a gdb session.

Tejas Kokje

Sep 18 '06 #6

P: n/a
"Ancient_Hacker" <gr**@comcast.netwrites:
code break wrote:
>Hi all,
Please can any one tell me the importance of below code...

#if 0 /*what is use of this macro conditional.since
this is every time false .*/

I usually define some symbol, something like:

#define Dont_Run_This_Code_Until_Hans_Finishes_His_Module 0000000000
Why 0000000000 rather than 0?

For that matter the negative name of the macro could cause
considerable confusion. Logically, the condition
"Dont_Run_This_Code_Until_Hans_Finishes_His_Module " is true, but
you're defining it as false.

This might be clearer:

#define HANS_HAS_FINISHED_HIS_MODULE 0

#if HANS_HAS_FINISHED_HIS_MODULE
....
#endif

Or this:

#undef HANS_HAS_FINISHED_HIS_MODULE

#ifdef HANS_HAS_FINISHED_HIS_MODULE
....
#endif

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 18 '06 #7

P: n/a

Keith Thompson wrote:
"Ancient_Hacker" <gr**@comcast.netwrites:
code break wrote:
Hi all,
Please can any one tell me the importance of below code...

#if 0 /*what is use of this macro conditional.since
this is every time false .*/
I usually define some symbol, something like:

#define Dont_Run_This_Code_Until_Hans_Finishes_His_Module 0000000000

Why 0000000000 rather than 0?

For that matter the negative name of the macro could cause
considerable confusion. Logically, the condition
"Dont_Run_This_Code_Until_Hans_Finishes_His_Module " is true, but
you're defining it as false.
You're right, I have a touch of dyslexia. Then again, I used to work
with someone whose native language encouraged multiple negatives. So
you'd get prompts from their program, really, like this:

Do you not want to not end not running this chapter nomore?

Sep 18 '06 #8

P: n/a
"Ancient_Hacker" <gr**@comcast.netwrites:
[...]
You're right, I have a touch of dyslexia. Then again, I used to work
with someone whose native language encouraged multiple negatives. So
you'd get prompts from their program, really, like this:

Do you not want to not end not running this chapter nomore?
Press any key to continue. Press any other key to quit.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 18 '06 #9

P: n/a
code break wrote:
> Please can any one tell me the importance of below code...

#if 0 /*what is use of this macro conditional.since
this is every time false .*/
stmt1 ;
#else
stmt2;
#endif

Is it any reason to include above macro conditional in program ?
jmcgill <jm*****@email.arizona.eduwrites:
(Rather than mess with comment blocks, you can just change the 0 to
1, and have the alternative.)
This goes even further. You can't use comments if you want to disable
code which contains comments since comments cannot be nested and #ifs
can. (argh.. so many "comments"...)

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>--<jid:mina86*jabber.org>--ooO--(_)--Ooo--
Sep 18 '06 #10

P: n/a
On Mon, 18 Sep 2006 16:20:53 +0200, jacob navia
<ja***@jacob.remcomp.frwrote:
>code break wrote:
>Hi all,
Please can any one tell me the importance of below code...

#if 0 /*what is use of this macro conditional.since
this is every time false .*/

stmt1 ;

#else

stmt2;

#endif
Is it any reason to include above macro conditional in program ?

Yes, it is an easy way to invalidate a block of code

#if 0
// All code here will be invalidated (not active)
What did you intend invalidated (or validate below) to mean? I could
not find either term in the standard dealing with the preprocessor.

This code will not be compiled.
>// If there is an #else clause it will be validated
This code will be compiled.
>#endif

Remove del for email
Sep 19 '06 #11

P: n/a

code break wrote:
Hi all,
Please can any one tell me the importance of below code...

#if 0 /*what is use of this macro conditional.since
this is every time false .*/

stmt1 ;

#else

stmt2;

#endif
Is it any reason to include above macro conditional in program ?
Thanks all....

Sep 19 '06 #12

P: n/a
"Ancient_Hacker" <gr**@comcast.netwrote:
You're right, I have a touch of dyslexia. Then again, I used to work
with someone whose native language encouraged multiple negatives. So
you'd get prompts from their program, really, like this:

Do you not want to not end not running this chapter nomore?
Afrikaner?

Richard
Oct 2 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.