A Not So Stupid C Mistake

I was reading through a website of "computer stupidities" today when I came across this,

if (a)
  {
    /* do something */
    return x;
  }
else if (!a)
  {
    /* do something else */
    return y;
  }
else
  {
    /* do something entirely different */
    return z;
  }

This was quickly dismissed as being an obvious beginner mistake. I don't think this can be dismissed so quickly without thinking it through for a moment. Yes, in the example above we will never reach the last condition where we return z, but consider the following,

if (a < b)
  printf ("foo\n");
else if (a > b)
  printf ("bar\n");
else if (a == b)
  printf ("baz\n");
else
  printf ("faz\n");

The same quick dismissal might drop the last "faz" print statement as being an impossible condition. Can you think of a situation where the program would print "faz"?

Our final condition will be reached if a or b is equal to NAN, which is defined by the IEEE floating-point standard. It is available in C99 from math.h. A NAN in any of the comparisons above will evaluate to false.

So don't be so quick to dismiss code like this.

Have a comment on this article? Start a discussion in my public inbox by sending an email to ~skeeto/public-inbox@lists.sr.ht [mailing list etiquette] , or see existing discussions.

null program

Chris Wellons

wellons@nullprogram.com (PGP)
~skeeto/public-inbox@lists.sr.ht (view)