C Puzzler – Like Java Puzzlers, but for C!

I’m a big fan of Java Puzzlers. If you’re not familiar with what those are, I highly encourage you to watch this Google Tech Talk by Joshua Bloch. In short, Java Puzzlers are quirky pitfalls and corner-cases one might encounter when programming with the Java language. I liked the concept so much, I thought I’d add a section on my blog for my own puzzlers! The format is simple:

  1. Code – I introduce the code
  2. Question – I pose a multiple-choice question and you guess what the outcome is…think hard!
  3. Walkthrough – I walk through a reasonable explanation
  4. Answer – I tell you the real outcome (it might surprise you), and explain why
  5. Moral – How can we avoid making mistakes like this in our own code

Now that we know what a Puzzler is, here is a simple C Puzzler:


/** c-puzzler-lets-get-funcy.c **/
#include <stdio.h>
int funcOne();
int funcTwo();
int x = 1;
int main()
	int y = funcOne() + funcTwo();
	return y;
int funcOne()
	return x;
int funcTwo()
	return ++x;


What value does main return?

  1. 2
  2. 3
  3. 4
  4. it varies


There is a global variable, x, and it is initialized to 1. In main(), there is a local variable, y, and it is initialized to the value returned by funcOne() plus the value returned by funcTwo(). The function funcOne() simply returns the value stored in variable x, which is 1. The function funcTwo() also references x, but increments it in the process. Since the increment operator is a prefix, it performs the increment before returning the value, and so funcTwo() will return the incremented value of x, which is 2. Therefore, y will equal 1 + 2, which is 3, and main() will return the int value 3. So, my answer is b, main will return 3.



















The answer is d – it varies. This is because in ANSI C, there is no guarantee as to the order in which the operands of an operator are evaluated. So, depending on the compiler implementation, funcOne() can be evaluated before funcTwo(), and vice versa, giving us a result of 3 or 4 depending on the compiler. It should be noted, though, that MOST C compilers will evaluate expressions of operands left to right, including the GNU compiler, but there is no guarantee that this is the case. As a result, the safest way to achieve compiler-agnostic results would be to use intermediary variables.


The order in which operands of an operator is unspecified in C. So, to give safe and predictable results, external variables should be modified carefully, and specifically noted in the function’s documentation. If the documentation for a function is lacking, or you are working with a 3rd party API which, for all intents and purposes, is a black-box, use of intermediary variables to ensure order of operations is safest.

I hope you’ve enjoyed my first C Puzzler! Until next time, happy coding!


c-puzzler-lets-get-funcy.zip (source code)

2 Responses to C Puzzler – Like Java Puzzlers, but for C!

  1. Thanks for this, Charles! I find these puzzlers really fun and hope you keep them coming.

    Just to add a bit more details:
    “Only the sequential-evaluation (,), logical-AND (&&), logical-OR (||), conditional-expression (? :), and function-call operators constitute sequence points and therefore guarantee a particular order of evaluation for their operands.”

    Therefore relying on left-to-right evaluation for operators like && and || is perfectly legal, and is in fact frequently used. Especially in C++, for example:

    if (obj != null && !obj.isEmpty()) { … }

    (Got the text from the first Google hit I got on the topic: http://msdn.microsoft.com/en-us/library/2bxt6kc4(VS.71).aspx )

  2. Charles Bihis says:

    Yes! That is a very important distinction. That is exactly how we have language features like short-circuit evaluation. Thanks for that, Philippe!