2

Is it recommended in c to write conditionals like this

if(foo != 0) {
    // do stuff
}

Or like this

if(foo) {
  // do stuff
}

I know they will always produce the same result, but which method is recommended for readability?

6
  • It depends on who you ask. Then there is if ( 0 != foo ) {. Commented Mar 18, 2015 at 21:33
  • 4
    No way I'm writing that though... Commented Mar 18, 2015 at 21:34
  • I typically use the first when foo is an integer quantity that can have many values, and the second when foo is a boolean (1/0, yes/no, true/false) quantity. Not to say that foo has to be declared as a bool, but simply that foo has the semantics of a boolean. Commented Mar 18, 2015 at 21:43
  • The way @RSahu has been explained may seem odd, but has the advantage of avoiding one silly and recurrent mistake that happens with equality comparisons: if (foo=0) which not only evaluates always as false, but it changes the value of foo when it shouldn't. If you use to write comparisons placing the constant operand first, then the operator, then the other operand, an attempt to write: if (0=foo) will cause the compiler to trigger the obvious error Commented Mar 19, 2015 at 2:05
  • I know all that, I just think Yoda conditions are less readable Commented Mar 19, 2015 at 14:23

2 Answers 2

5

Consider the various types that foo may have. What is best is context sensitive.

I favor the first in each group.

void *fooptr = bar();
if (fooptr) ...
if (fooptr != NULL) ...
if (fooptr != 0) ...

int fooi = bar();
if (fooi != 0) ...
if (fooi) ...

double foofp = bar();
if (foofp != 0.0) ...
if (foofp) ...

bool foobool = bar();
if (foobool) ...

int (*foofun)() = bar();
if (foofun) ...
if (foofun != NULL) ...
if (foofun != 0) ...

But avoid foolish ones

if (foobool == 0.0) ...
if (fooi != NULL) ...

And I did not even touch the Yoda style (trying to avoid a holy war)

if (0 == foo) ...

In general, seeing code in the positive sense if(foo) vs. if (foo != 0), is conceptually simpler. And I prefer if(foo == NULL) to if(!foo)

You not think it isn't less understandable to avoid negating negatives, no?

Sign up to request clarification or add additional context in comments.

3 Comments

Could you please elaborate why if (foofp != 0.0) ... // is not the same as if (!(foofp == 0.0)) ... ?
Aha, okay, but if foofp is a number then? Are they not the same? Or are they ALWAYS the same? @chux-reinstate-monica
@Snakehater Always is a long time. Both are the same, when foofp is infinite or finite like 0 or 42.123. When a NaN is involved, a != b is commonly the same as !(a == b), yet C is squishy on that point (its an implementation detail). IEEE 754 specifies that are the same, but C does not specify IEEE 754 adherence.
0

I believe that if(foo != 0) is more clear. It some how also shows that foo is something that can be compared to 0.

When I see if(foo) is foo a bool or is it something else?

1 Comment

@joris_van_winden: There are _Bools, and with stdbool.h they are called bool like in C++. See en.cppreference.com/w/c/types/boolean

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.