'Which one is safer to use? " ==TRUE" or " != FALSE"
Is it better to compare a boolean type variable with:
== FALSE
and!= FALSE
; or== TRUE
and!= TRUE
?
Solution 1:[1]
Is it better to compare a boolean type variable with " == FALSE" and " != FALSE" or with " ==FALSE" and " ==TRUE" ?
- Neither.
With the C boolean type _Bool
, do not use ==
or !=
to assess truth-ness against a constant.
@Steve Summit @Antti Haapala @John Bode
_Bool x;
if (x) // to assess truth-ness
if (!x) // to assess false-ness
If a strong desire remains to use
==
,!=
, like many style issues, best to follow your group's coding guidelines.Lacking group's coding guidelines - make them.
Use <stdbool.h>
as able to access bool, true, false
rather than code TRUE
, FALSE
.
@Eugene Sh.
Which one is safer to use? “ ==TRUE” or “ != FALSE”
Note that comparing a == TRUE
can fail unexpectedly as the operands are compared as integers, FP or pointers, not as boolean. This may fail to compare truth-ness should a
be a non-boolean with a "truth" value other than 1 even if TRUE
is a boolean 1.
double a = 0.999999;
// Both are false
if (a == TRUE) { ... } // The arithmetic value of `a` is used, not its "truth"
if (a == FALSE) { ... }
// better as
if (a) { ... } // As if `a != 0`
else { ... }
Consider cases where the "truth" is returned as non-zero, perhaps not 1.
if(islower('a') == TRUTH) ... // The if() block might or might not execute
if(islower('a')) ... // The if() block will execute
a != 0
or a != false
tends to be safer.
Style: I find ==
code easier to follow than !=
as negations add mental complexity for people. Example
Solution 2:[2]
TRUE
andFALSE
is not standard C and should therefore not be used, unless perhaps you are coding against an old C90 library that uses them (such as Windows API).- The boolean type in C is
_Bool
. It can be used asbool
and should be set to valuestrue
orfalse
fromstdbool.h
- The need to explicitly compare a boolean against true/false suggests poor variable naming. A somewhat common naming standard for booleans is to prefix them with
is
, such as:while(!isReady) { ... }
. - The
==
and!=
operators are equally safe. Both could in theory be mixed up with=
and=!
, but any decent compiler will warn for that ("assignment in expression"/"possible incorrect assignment").
Solution 3:[3]
Everey syntax has its own meaning and there is not better or safer way. But you should always consider these special points in comparison to decide which syntax is safer in your case:
- in C language NUMBERS represent booleans: 0 is FALSE and other numbers are TRUE. You may restrict to
stdbool.h
to prevent mixing up integers and boolean. - if you connect to SQL databse uisng C , in SQL, a comparison against NULL is not true nor false.
- If are working in team I suggest to all team members to use
if (something==TRUE)
rather thanif(something)
because indicates that you mean boolean fromsomething
not an Integer. Meke sure that your temameates agree with this syntax and also make sure that you have considered case number 1!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Lundin |
Solution 3 |