Defining Success

I’ve had two people today ask about my shirt. It contains a single line of C code:

while(!(succeed = try()));

Even the non computer savvy should be able to figure out that it’s a geeky way of stating the old adage “If at first you don’t succeed, try, try again.” But how do I explain the strange syntax to a non-programmer?

First off, even though this is the way most C programmers would write it (and, more importantly, is the way most Linux and BSD kernel developers would write it), a programming teacher would mark it wrong on a test. The “better” and more human readable way to write it would be:

int succeed = 0;
while (succeed == 0)
{
     succeed = try();
}

I added the explicit declaration only for the sake of completeness. The t-shirt assumes that there exists at the very least a function prototype called int try(void) and an integral variable called succeed. Let’s condense this line by line:

First off, without getting into the technicalities of how C treats arbitrary blocks of code bounded by curly braces, we can eliminate the curlies here altogether since we’re only executing a single statement inside the while loop:

int succeed = 0;
while (succeed == 0)
     succeed = try();

Secondly, assuming we’re testing for succeed being nonzero (which is the usual case when checking for the success of a function that returns an integral value), we can eliminate the explicit equality conditional (==) and replace it with a zero test:

int succeed = 0;
while (!succeed)
     succeed = try();

The “!” operator is logical NOT, meaning it inverts the zero-ness of a variable. For this example, we’re asking the program to loop as long as succeed is not logically “true.” If succeed is zero (false), the ! operator inverts the value to true. This true is then evaluated and the while iterates. If succeed is anything other than zero (true), the ! operator inverts the value to false. The false is then evaluated and the while terminates.

Fourth, we can take advantage of the universality of C’s assignment operator and inside-out nesting resolution by actually assigning the variable value within the same statement as the while condition:

int succeed = 0;
while (!(succeed = try()));

Since C evaluates expressions from the inside out, the order would read thus:

  1. Call the function try() with no arguments.
  2. Store the result in the variable called succeed.
  3. Consider the value returned from the assignment operation (i.e. the new value of succeed) – this is the meaning of the parentheses enclosing the succeed = try() statement.
  4. Invert the logic value.
  5. Test for truth.
  6. Iterate if true.

Finally, tighten up whitespace and get rid of the variable declaration (which the T-shirt omits for the sake of brevity):

while(!(succeed = try()));
Advertisements

, , ,

  1. #1 by Gary on August 31, 2012 - 12:37 PM

    Brilliant. Thank you for explaining the C logic. I am familiar with Jquery, Javascript, and Actionscript. But this is a lovely bit of code that i’m going to put on a tshirt as well!

    I saw Intel post this on their Facebook page the other day and it blew my mind.

  2. #2 by Trolfa on May 28, 2013 - 7:36 PM

    Yeah.. but what about the function definition of try? I don’t see that anywhere, and it’s certainly a function, not the keyword try.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: