CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Does #elif work as intended

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
picnic



Joined: 09 Oct 2008
Posts: 15

View user's profile Send private message

Does #elif work as intended
PostPosted: Wed Dec 28, 2011 11:18 am     Reply with quote

A

Code:
#ifdef WIBBLE


only requires that WIBBLE be defined, can be 0 or any other value

If you try a

Code:
#elif WIBBLE


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

View user's profile Send private message

PostPosted: Wed Dec 28, 2011 11:33 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Dec 28, 2011 11:49 am     Reply with quote

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 Confused

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

View user's profile Send private message

PostPosted: Wed Dec 28, 2011 1:41 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Dec 28, 2011 5:46 pm     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group