|
|
View previous topic :: View next topic |
Author |
Message |
allenhuffman
Joined: 17 Jun 2019 Posts: 552 Location: Des Moines, Iowa, USA
|
while(1) versus while(TRUE) - compiler warnings |
Posted: Mon Feb 03, 2020 4:01 pm |
|
|
Why does this generate a compiler warning...
...but this does not?
But TRUE is defined as:
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
|
|
Posted: Mon Feb 03, 2020 4:37 pm |
|
|
There must be, and people that know the compiler just use TRUE. |
|
|
allenhuffman
Joined: 17 Jun 2019 Posts: 552 Location: Des Moines, Iowa, USA
|
|
Posted: Mon Feb 03, 2020 4:42 pm |
|
|
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: 9229 Location: Greensville,Ontario
|
|
Posted: Mon Feb 03, 2020 5:24 pm |
|
|
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..... |
|
|
newguy
Joined: 24 Jun 2004 Posts: 1908
|
|
Posted: Mon Feb 03, 2020 9:39 pm |
|
|
temtronic wrote: | Ah, but what IS 'TRUE' !? |
This is the type of question which is often pondered whilst under the influence of that which is now legal in Canada. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Tue Feb 04, 2020 1:31 am |
|
|
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. |
|
|
allenhuffman
Joined: 17 Jun 2019 Posts: 552 Location: Des Moines, Iowa, USA
|
|
Posted: Tue Feb 04, 2020 8:25 am |
|
|
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 ? |
|
|
|
|
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
|