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

C

code break

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 ?
 
J

jacob navia

code said:
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
 
C

Clever Monkey

code said:
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.
 
A

Ancient_Hacker

code said:
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
 
J

jmcgill

code said:
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.
 
T

Tejas Kokje

code said:
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
 
K

Keith Thompson

Ancient_Hacker said:
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
 
A

Ancient_Hacker

Keith said:
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?
 
K

Keith Thompson

Ancient_Hacker said:
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.
 
M

Michal Nazarewicz

jmcgill said:
(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"...)
 
B

Barry Schwarz

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.


Remove del for email
 
C

code break

code said:
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....
 
R

Richard Bos

Ancient_Hacker said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,123
Latest member
Layne6498
Top