View previous topic :: View next topic |
Author |
Message |
picnic
Joined: 09 Oct 2008 Posts: 15
|
Does #elif work as intended |
Posted: Wed Dec 28, 2011 11:18 am |
|
|
A
only requires that WIBBLE be defined, can be 0 or any other value
If you try a
the following code is not included if WIBBLE is defined as 0, it's OK with 1
Seeing as the #ifdef only required the definition to exist without any care for its value it seems logical the the #elif would behave the same way.
Try
Code: |
void main() {
int i;
#ifdef OPT1
i = 10;
#endif
#ifdef OPT2
i = 20;
#elif OPT1
i = 11;
#else
i = 30;
#endif
}
|
if #OPT1="0" is included in the compilation command the i=11 does not compile (the i=10 is fine), with #OPT1="1" both pieces of code compile.
Compiler version is 4.128 and I'm targeting a 18F46K22 if it makes any difference. |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Wed Dec 28, 2011 11:33 am |
|
|
I think elif goes with if. To go with ifdef you would want elifdef which I have never seen. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
picnic
Joined: 09 Oct 2008 Posts: 15
|
|
Posted: Wed Dec 28, 2011 11:49 am |
|
|
I like your thinking, the example in the help though is
Code: | #IFDEF id
code
#ELIF
code
#ELSE
code
#ENDIF
|
which makes no sense (as there is no condition for the #elif) but does imply #elif is for #ifdef
Right just tried your idea. No compiler errors using #elifdef but the i=11 doesn't get compiled for OPT=1 or OPT=0 so appears to be worse. |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Wed Dec 28, 2011 1:41 pm |
|
|
As far as I see is the CCS implementation of preprocessor if-section compliant to the C language standard.
There's no elifdef provided in the standard.
The misleading help file examples should be in fact corrected.
The logical way for alternative options in conditional compilation is to use non-zero constant expressions, I think.
They can be queried with #if #elif #else #endif. |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Wed Dec 28, 2011 5:46 pm |
|
|
i think
#ELIF defined(someDefine)
or
#ELIF !defined(someDefine)
Should work.
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
|