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)

I’ll trade you…a joke for a book

My name is Charles and this is my blog.

So, I’ve decided to start an official blog on Adobe Blogs.  Since I graduated (and even while in university), I’ve tried to contribute to the community as much as I could, but lately, I’ve been…well…lazy.  This blog is my attempt at getting back at it, for better or for worse : P

I thought, what should my first (really second) post be about?  I figured it should be something engaging, like asking people to guess approximately how many lines of code there are in CS5 (which we just recently released), or asking people to submit some ActionScript code to do something like sort a list of 10,000 numbers, most efficient gets a prize.  But, I thought that these are kinda cliché and kinda boring…let’s do something fun.

How about this:

Add a comment to this post with your favorite tech joke.  On June 4th, 2010 (two weeks from today), myself and an impartial jury (i.e. my team-mates) will choose the best ones, and I’ll send you a book!

books.jpgI have a bunch of books ready to ship out, mostly about Flash, Flex, AIR, and ActionScript, but I obviously can’t give one to everyone.  So, top 5 will definitely get something, and, if shipping isn’t killing me, runners up will get consolation prizes!  That’s it!

Happy commenting!

*Important: Make sure you put a valid e-mail address so that I can contact you if you’re a winner!

*Update: There were only 7 entries, so you ALL get a book : )  For anyone reading this post after today, feel free to add more…these are GREAT!

Hello, Interwebs!

Hello, Interwebs.  My name is Charles.