Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 6th, 2004, 07:32 PM
Authorized User
 
Join Date: Sep 2003
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default conditional prepocessor directives

Can somebody please explain to me why someone would use a conditional prepocessor directive instead of a normal if and else construct.. I can't seem to understand the concept properly..

ie why....


#if !something
     //do something
#else
     // do something else
#endif

ie when you can do this.....


if ( !something )
{
     //do something
}
else
{
     // do something else
}


I know that the former is used for avoiding compilation for bits of code altogether whereas the latter compiles everything..
BUT SO WHAT!! does it really matter wether something is compiled or not.... as long as the end result is the same....

please explain in detail... much appreciated...........!!

sincerely,
Ibn_Aziz


 
Old February 6th, 2004, 09:08 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

yes, it matters incredibly. Using preprocessor directives to avoid having code compiled will allow you to write code in a file that can't be compiled on some systems but is compilable on others.


Consider any application that's written to be cross-platform compatible. It's obvious that the OS API is different for Windows than it is on Linux, but if I wanted to write an application that can be compiled on both machines, I'd use the #if/#else preprocessor directives to allow this.

You also might want to conditionally execute other preprocessor directives.


Download pretty much ANY open-source C/C++ cross-platform application and look through any of the source/header files. You're bound to see lots of #ifdef, #if defined, #elif, and #else statements.


Take care,

Nik
http://www.bigaction.org/
 
Old February 7th, 2004, 02:33 AM
Authorized User
 
Join Date: Sep 2003
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you for the reply,

What I am still confused about is that doesn't the same happen with a normal if else construct....

so if one part the construct is true the other remains false..... and as a result doesn't execute..... so doesn't this do the same thing..?
 
Old February 9th, 2004, 12:24 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Using a standard if...else creates a runtime condition. The code within both conditions is still compiled. What nik is that you may not what sections compiled. If you have code that is OS specific, it may very well not compile on another OS. So you can't use a standard if...else because all of it is compiled.

I have used prepocessor conditions to have sections of an application compiled when I'm building it in the debug configuration versus the release configuration.

Why compile code that is never executed?

Peter
------------------------------------------------------
Work smarter, not harder.





Similar Threads
Thread Thread Starter Forum Replies Last Post
regarding conditional statements abhi.prince4u XSLT 1 July 21st, 2007 06:00 AM
Conditional formatting vemaju Excel VBA 2 June 6th, 2007 04:41 AM
conditional IF roy_mm Reporting Services 0 October 30th, 2006 07:52 AM
Caching directives in webserver manuelam BOOK: Professional Apache Tomcat 0 June 16th, 2006 02:18 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.