## Loopy C puzzle

Let me share a very interesting C programming puzzle that I learnt from one of my colleagues.

Consider the following code-snippet written in the C programming language.

int i;
for (i = 0; i < 6; i--)
printf(".");

This code invokes undefined behaviour. The value in variable i decrements to INT_MIN after |INT_MIN| + 1 iterations. In the next iteration, there is a negative overflow which is undefined for signed integers in C. On many implementations though, INT_MIN − 1 would result in INT_MAX which is not less than 6 and thus the loop would terminate. With such implementations, this code would print |INT_MIN| + 1 dots. With 32-bit integers, that would be 2147483649 dots. However, it could also be optimized into an endless loop by the compiler if it wants to exploit the fact that a negative overflow for signed integers is undefined. gcc in fact does optimize that to an endless loop if you compile it with the -O3 option.

Now, here is the puzzle.

Add or modify exactly one operator in the following code such that it prints exactly 6 dots.

int i;
for (i = 0; i < 6; i--)
printf(".");


An obvious solution is:

int i;
for (i = 0; i < 6; i++)
printf(".");


There are at least 3 other solutions. I found one of them very interesting. I'll discuss the interesting solution tomorrow. Till then, feel free to try and post your solutions below as comments.

Update: The discussion on the solution is now available at: Solutions to 'Loopy C Puzzle'.

#### Ryan said:

int i;
for (i = 0; abs(i) < 6; i--)
printf(".");


#### Ryan said:

int i;
for (i = 0; -i < 6; i--)
printf(".");


#### Sean said:

Changing the loop condition to i ^= 6; is another solution.

#### Ryan said:

Ah-ha, a tricky one:

int i;
for (i = 0; i ^= 6; i--)
printf(".");


#### Ryan said:

Ah, Sean beat me to it. :(

#### Martin DeMello said:

for (i = 0; i + 6; i--)


will stop when i + 6 = 0.

#### Susam Pal said:

All solutions except the one in the first comment are correct. The first one involves abs() function. The code would indeed end up printing 6 dots but it doesn't meet the requirement in the question since abs() is not an operator.

There is another very simple solution similar to the obvious solution I have provided in the post. Instead of post-increment operator, use the pre-increment operator.

int i;
for (i = 0; i < 6; ++i)
printf(".");


The one involving the bitwise XOR operator is the one I had in my mind while mentioning that one of the solutions is very interesting. I find it interesting because it is not obvious at the first glance why it works. Another curious property of this solution is that it works only for an even number of dots. I'll write about this in another post.

#### Ryan said:

Susam, absolute value is a mathematical operator. You didn't specify the set of acceptable operators. :P

#### Susam Pal said:

Ryan,

I am used to referring to |x| as the absolute value function. But yeah, I see your point. The terminology is what we make it.

The problem statement could have specified that exactly one operator from the C programming language must be added to or modified in the given code snippet. That would have ruled out this little ambiguity.

#### Anonymous said:

Can someone explain how does the bitwise XOR work? Looks interesting.

#### Susam Pal said:

Yes, I have posted a detailed explanation of the solution involving bitwise XOR assignment operator here: Solutions to 'Loopy C puzzle'.