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 support@ccsinfo.com

while(1) versus while(TRUE) - compiler warnings

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



Joined: 17 Jun 2019
Posts: 537
Location: Des Moines, Iowa, USA

View user's profile Send private message Visit poster's website

while(1) versus while(TRUE) - compiler warnings
PostPosted: Mon Feb 03, 2020 4:01 pm     Reply with quote

Why does this generate a compiler warning...

Code:
while(1);


...but this does not?

Code:
while(TRUE);


But TRUE is defined as:

Code:
#define TRUE 1


I assume there's a special detection in the compiler for TRUE?

Quote:
>>> Warning 203 "C:\Users\huffm\Perforce\allenh\Users\AllenHuffman\PIC\Structure\main.c" Line 17(1,1): Condition always TRUE

_________________
Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Feb 03, 2020 4:37 pm     Reply with quote

There must be, and people that know the compiler just use TRUE.
allenhuffman



Joined: 17 Jun 2019
Posts: 537
Location: Des Moines, Iowa, USA

View user's profile Send private message Visit poster's website

PostPosted: Mon Feb 03, 2020 4:42 pm     Reply with quote

PCM programmer wrote:
There must be, and people that know the compiler just use TRUE.


I do a lot of cross-platform code, where "TRUE" does not exist. Fortunately, <stdbool.h> does and it also honors the standard C "true" define without a warning (when using #case), so that will allow portable code without making a new define only used on the other systems.
_________________
Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ?
temtronic



Joined: 01 Jul 2010
Posts: 9120
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Feb 03, 2020 5:24 pm     Reply with quote

Ah, but what IS 'TRUE' !?
While some (most) think of 'true' to be '1', nothing says you can't be using negative logic and as such 'true' would be '0'.
Just consider the classic 'push button' configuration with a pullup resistor. It's normal 'off' is a '1', activated it's a '0'.
Then there's RS232..... Smile
newguy



Joined: 24 Jun 2004
Posts: 1903

View user's profile Send private message

PostPosted: Mon Feb 03, 2020 9:39 pm     Reply with quote

temtronic wrote:
Ah, but what IS 'TRUE' !? Smile


This is the type of question which is often pondered whilst under the influence of that which is now legal in Canada. Laughing
Ttelmah



Joined: 11 Mar 2010
Posts: 19238

View user's profile Send private message

PostPosted: Tue Feb 04, 2020 1:31 am     Reply with quote

Historically, on early compiler versions neither generated an warning.
Then CCS started adding more 'logical syntax' checking, and they added
the warning for a perpetual loop.
At this point some people here (I was one of them), 'moaned' that it
was rather silly to have warnings on things like the permanent loop
that was a necessary part of CCS C to prevent the drop off the end.
CCS responded by adding the 'exception' to the warning if 'TRUE' was
used. This works well, and given that it is a very 'deliberate' construct,
makes it easy to avoid the warning when required.

If moving the code to another compiler that doesn't support 'TRUE',
then simply #define this. However definition of this is required in C now
(#include <stdbool.h> is the 'reliable' way to ensure this is defined).
In fact the CCS definition as TRUE being '1' is really a little 'naughty',
the recommended way of defining TRUE and FALSE is:
Code:

#define FALSE (0)
#define TRUE (!FALSE)

Which then ensures the definitions function correctly.
C as part of the language defines TRUE as being 'not zero'. So the
negative logic argument doesn't apply in C. Very Happy
allenhuffman



Joined: 17 Jun 2019
Posts: 537
Location: Des Moines, Iowa, USA

View user's profile Send private message Visit poster's website

PostPosted: Tue Feb 04, 2020 8:25 am     Reply with quote

I enjoy it when I see this:

Code:
#define TRUE (1==1)
#define FALSE (1==0)


"There. Whatever it is, this generates it!"

As a youngun' learning C in the late 1980s on an 8-bit home computer, I often got my TRUE/FALSE defines mixed up. But, as long as code said "if (var == TRUE)" it worked... It was years later that I realized what TRUE/FALSE should really be.
_________________
Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ?
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