473,398 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

TRUE/FALSE values: Point / Counterpoint

2,426 Expert 2GB
A side-discussion developed in the Wierd Visual Studio Problem thread. Rather than continue to hijack that topic, I've created a new thread and copied over the relevant postings.
@Andr3w
Mar 20 '09 #1
14 3846
donbock
2,426 Expert 2GB
@donbock
@donbock
....................
Mar 20 '09 #2
donbock
2,426 Expert 2GB
@Andr3w
....................
Mar 20 '09 #3
donbock
2,426 Expert 2GB
@donbock
....................
Mar 20 '09 #4
donbock
2,426 Expert 2GB
@Andr3w
....................
Mar 20 '09 #5
donbock
2,426 Expert 2GB
I don't think this is an issue of coding style. Consider the following code:
Expand|Select|Wrap|Line Numbers
  1. #define TRUE 0
  2. #define FALSE 1
  3.  
  4. int isDone;
  5. ...
  6. if (isDone) {
  7.    <something that only happens when you're done>
  8. }
If you set isDone to TRUE then the conditional code doesn't execute; if you set isDone to FALSE then the conditional code executes. This is counterintuitive behavior. You get similar unexpected behavior if you use isDone as the condition to terminate a loop.

C99 added <stdbool.h> as a new standard header. This header is required to contain something equivalent to:
Expand|Select|Wrap|Line Numbers
  1. #define true 1
  2. #define false 0
The chances of confusion and error abound when "true" means the same thing as "FALSE".
Mar 20 '09 #6
Andr3w
42
Well, I am careful with my code in order to get it working and bug free. I have a coding style and you have yours. It's a matter of preference and I know this behavior is present if I set the values that way...

Anyway really I don't think there is a point continuing this really as there is no argument to begin with, or at least I think so
Mar 20 '09 #7
JosAH
11,448 Expert 8TB
@donbock
That makes -2 points (-1 for each semicolon). Next!

kind regards,

Jos
Mar 20 '09 #8
Andr3w
42
weeee thanks for pointing it out...stupid typos are a common mistake...for all programmers...

Anyway I'll be more careful next time...to avoid typos

be safe
Mar 20 '09 #9
JosAH
11,448 Expert 8TB
@Andr3w
Also better turn those definitions around, as in:

Expand|Select|Wrap|Line Numbers
  1. #define TRUE 1
  2. #define FALSE 0
  3.  
I, for one, rely on the fact that TRUE is a non-zero value and 1 to be exact; I like to use that value in arithmetic expressions when appropriate. Also I expect a true expression to succeed in a conditional expression. Even more I want the following to succeed as well:

Expand|Select|Wrap|Line Numbers
  1. if ((1+1 == 2) == TRUE) { ... }
  2.  
kind regards,

Jos
Mar 20 '09 #10
weaknessforcats
9,208 Expert Mod 8TB
Defining TRUE = 1 and FALSE = 0 won't work. Never has worked. Remember that 25 is also TRUE.

You need to do it this way:
Expand|Select|Wrap|Line Numbers
  1. #define FALSE  0
  2. #deifne TRUE  !FALSE
Mar 20 '09 #11
JosAH
11,448 Expert 8TB
@weaknessforcats
True (sic) but the canonic integer value for 'true' equals 1. Read the Standard (e.g. for the ! operator):

The result of the logical negation operator ! is 0 if the value of
its operand compares unequal to 0, 1 if the value of its operand
compares equal to 0. The result has type int . The expression !E is
equivalent to (0==E) .
Here's another (more realistic) example:

Expand|Select|Wrap|Line Numbers
  1. #define SIGN(x) (((x) > 0) - ((x) < 0))
  2.  
I can use that integer value in integer expressions if I want to so I expect any symbolic value TRUE to equal 1.

kind regards,

Jos
Mar 20 '09 #12
weaknessforcats
9,208 Expert Mod 8TB
I have no idea about canonical or not but this doesn't work as expected:

Expand|Select|Wrap|Line Numbers
  1. #define TRUE 1
  2. #define FALSE 0
  3.  
  4. int data = 25;
  5. if (data == TRUE)
  6. {
  7.         printf("It's true\n");
  8. }
TRUE has to be !FALSE
Mar 20 '09 #13
JosAH
11,448 Expert 8TB
@weaknessforcats
Then it still doesn't work; you are mixing up the (conceptual) boolean domain with the integer domain. btw !FALSE will be equal to 1.

kind regards,

Jos
Mar 20 '09 #14
donbock
2,426 Expert 2GB
I'm pretty sure that the Standards all guarantee that any logical expression will evaluate to precisely "0" or "1" so JosAH's SIGN macro is portable and safe.

On the other hand, conditional instructions (if, for, while) consider all nonzero argument values to be equivalent. I'm paranoid that a boolean variable might somehow be assigned a nonzero value other than "1". If that were to happen then explicit comparisons to canonical TRUE and FALSE values would see a value that was neither true nor false, possibly resulting in some weird inconsistent path through a series of conditionals.

I don't allow my team to perform comparisons to a canonical TRUE value. I have them either treat boolean variables as predicates or perform all comparisons against the canonical FALSE value.
Expand|Select|Wrap|Line Numbers
  1. void func1(int isThatCondition) {
  2.    if (isThatCondition) {...}
  3. }
  4.  
  5. void func2(int isThatCondition) {
  6.    if (isThatCondition != FALSE) {...}
  7. }
My preference is to use booleans as predicates, being careful that the variable names include an appropriate existence verb.
Mar 20 '09 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

46
by: Scott Chapman | last post by:
There seems to be an inconsistency here: Python 2.3.2 (#1, Oct 3 2003, 19:04:58) on linux2 >>> 1 == True True >>> 3 == True False >>> if 1: print "true" ....
11
by: Arpan | last post by:
<% If(False) Then Response.Write("False") Else Response.Write("True") End If %> In the above code, the Else condition will be executed. Why?
14
by: Walter Dnes (delete the 'z' to get my real address | last post by:
I took a C course some time ago, but I'm only now beginning to use it, for a personal pet project. My current stumbling-block is finding an efficient way to find a match between the beginning of a...
48
by: Skybuck Flying | last post by:
Hi, I came across this C code which I wanted to understand etc it looked like this: if (-1) etc It made me wonder what the result would be... true or false ? In C and Delphi
33
by: Ruffin Bailey | last post by:
I coulda sworn I was given an explanation during an AppDev class years ago for VB6, but don't recall the answer. Why is it that -1 is True in Visual Basic (and now VB.NET)? Bit flags seem like...
59
by: Pierre Quentel | last post by:
Hi all, In some program I was testing if a variable was a boolean, with this test : if v in My script didn't work in some cases and I eventually found that for v = 0 the test returned True ...
90
by: John Salerno | last post by:
I'm a little confused. Why doesn't s evaluate to True in the first part, but it does in the second? Is the first statement something different? False print 'hi' hi Thanks.
40
by: nufuhsus | last post by:
Hello all, First let me appologise if this has been answered but I could not find an acurate answer to this interesting problem. If the following is true: C:\Python25\rg.py>python Python...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.