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

How to suppress warning: "Condition always FALSE"
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Ttelmah



Joined: 11 Mar 2010
Posts: 19605

View user's profile Send private message

PostPosted: Mon Oct 03, 2016 4:00 am     Reply with quote

Well spotted RF_Developer.

I must admit I was only looking at the original code as posted. Hadn't seen the later version. At least explains 'why'.....
septillion



Joined: 21 Jan 2010
Posts: 13

View user's profile Send private message

PostPosted: Wed Oct 05, 2016 6:36 am     Reply with quote

rikotech8 wrote:

Consider this example:
Code:
#define ADC_setUpAdc()  setup_adc(ADC_CLOCK_INTERNAL);\
                        setup_adc_ports(AN0_TO_AN2);
if(someCondition)
   ADC_setUpAdc();


Then you are right. That's one of the reasons I consider macro's as powerful but harmful and I like C++ way more. That fixes this case with the inline keyword.

But for now if you want to use a macro:
Code:

#define ADC_setUpAdc()  {setup_adc(ADC_CLOCK_INTERNAL);\
                        setup_adc_ports(AN0_TO_AN2);}


Or use the CCS C add on #inline which tries to mimic the C++ behavior.
Code:

#inline
void ADC_setUpAdc(){
  setup_adc(ADC_CLOCK_INTERNAL);
  setup_adc_ports(AN0_TO_AN2);
}


[quote="Ttelmah"][quote="septillion"]
Ttelmah wrote:
.
No. The _pass_ through the code will occur once. There will be no 'loop'.

Ow man, that's just nagging about the English language! Yes, the loop here is not needed because of it's static nature. No, the compiler will not generate a loop in machine code. But here in C I call a do while loop a loop which always loops at least once. If you write a for loop (not static) like
Code:

for(i = 0; i < value; i++){
  //do something
}

Not a loop because value might be 1 or even 0?
Ttelmah



Joined: 11 Mar 2010
Posts: 19605

View user's profile Send private message

PostPosted: Wed Oct 05, 2016 8:01 am     Reply with quote

There is a difference between something using a variable that 'might' loop, and something that can only ever execute once. Being pedantic is sometimes vital in programming.... Very Happy

However the answer that really was needed was the simple one of not using a non-looping loop, but just using a code block:

Code:

#define ADC_setUpAdc()  {setup_adc(ADC_CLOCK_INTERNAL);\
                        setup_adc_ports(AN0_TO_AN2);}
if(someCondition)
   ADC_setUpAdc();


This is what is in fact contained in the original 'loop', but just says treat this as one single piece of code. A useful construct inside macros in particular.

There is a similar warning here with regards to using brackets. This is why you will sometimes see numeric things in macros defined with brackets round them. Problem is that without these, after expansion a mathematical formula for example placed into another formula may not expand as originally expected, depending on the precedence of the operations that end up each side.

Important caveats.

There are others, which is why I like to use the old C 'rule' (originally suggested in K&R), or reserving 'ALL CAPITALS' for macros.
This has the advantage of warning you that a bit of code that you may think is a function, is actually a macro, and thereby alerting you to these possible problems.
septillion



Joined: 21 Jan 2010
Posts: 13

View user's profile Send private message

PostPosted: Wed Oct 05, 2016 9:32 am     Reply with quote

Like the ALL_CAPS rule Smile

And here indeed a loop was completely misplaced. But what about a piece of expandable code?

Code:

#define NUMBER_CONNECTED 3

for(i = 0; i < NUMBER_CONNECTED; i++){
  doSomethingWithNumber(i);
}

This can be very useful piece of code but I might change NUMBER_CONNECTED to 1 next time. This indeed renders the loop not looping and if the compiler is smart it will optimize the loop away but looking at the C code I will still call it a loop. Very Happy
Ttelmah



Joined: 11 Mar 2010
Posts: 19605

View user's profile Send private message

PostPosted: Thu Oct 06, 2016 1:37 am     Reply with quote

That's why it warns you. Smile

Makes sure it really is what you want. Warnings are perfectly acceptable. I'll even make a remark at the start of my code, saying that a particular warning is expected on line xxx, but hasn't been suppressed, so when I next compile the code, I won't worry.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
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