|
Subject:
|
conditional prepocessor directives
|
|
Posted By:
|
Ibn_Aziz
|
Post Date:
|
2/6/2004 6:32:22 PM
|
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
|
|
Reply By:
|
nikolai
|
Reply Date:
|
2/6/2004 8:08:22 PM
|
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/
|
|
Reply By:
|
Ibn_Aziz
|
Reply Date:
|
2/7/2004 1:33:15 AM
|
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..?
|
|
Reply By:
|
planoie
|
Reply Date:
|
2/9/2004 11:24:36 AM
|
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.
|