• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

C programming puzzle

iCyborg

Golden Member
The puzzle is taken from Tanya Khovanova's web page (she has a nice blog too, probably the only one I visit on a semiregular basis):

Code:
int i, n = 20;
for (i = 0; i < n; i--)
{
    printf("-");
}

The puzzle is to find 3 ways to make this C program print 20 dashes by changing or adding exactly one character.
Finding one way is easy, two isn't that hard either, but for the third way I had to look up some (for me) obscure syntax stuff on the internet.

More variations are to make it print 21 characters, and to print 1 character (this one is cute).

Perhaps people could put spoilers if they write solutions here...
 
Well here's the first one just to kick things off...

Code:
int i, n = 20;
for (i = 0; -i < n; i--)
{
    printf("-");
}

Here's another...

Code:
int i, n = 20;
for (i = 0; i < n; n--)
{
    printf("-");
}
 
Last edited:
how about this

Code:
int i, n = 20;
for (i&= 0; i < n; i--)
{
    printf("-");
}
 
Last edited:
I think you added/changed two characters there, and anyway in that form it is just my second one with an uninitialized i forced to 0 at the start of the loop. I can't do this in C# and don't have a C compiler, but I guess you could get away with it in C.

Edit: meh, I can't see where the complement operator gets you anywhere Net. Probably just tired. Hopefully someone will post the answer by morning 🙂.
 
I think you added/changed two characters there, and anyway in that form it is just my second one with an uninitialized i forced to 0 at the start of the loop. I can't do this in C# and don't have a C compiler, but I guess you could get away with it in C.

Edit: meh, I can't see where the complement operator gets you anywhere Net. Probably just tired. Hopefully someone will post the answer by morning 🙂.

🙂 woops I just copied one of the code blocks near the top, didn't get the ops. The &= 0 is the key as it sets i to zero in a different way.
 
Yeah, actually I remembered that 21 was the problem for me that I had to look up complement operator. I had to rediscover the third way, I'll post a hint:
use the fact that arithmetic expressions can be used as booleans

How about once? I kinda like that one, it's pretty smart...
 
Yeah, actually I remembered that 21 was the problem for me that I had to look up complement operator. I had to rediscover the third way, I'll post a hint:
use the fact that arithmetic expressions can be used as booleans

How about once? I kinda like that one, it's pretty smart...

I learned this a few years ago during an exercise to create the shortest password generator in PHP (same concept).

I posted:

PHP:
for($i=0;$r=rand(48,109),$i<9;$i++)$p.=chr($r>57?$r>83?$r+13:$r+7:$r); 
echo $p;

And someone replied:

PHP:
for($i=9;$r=rand(48,109),$i;--$i)$p.=chr($r>57?$r>83?$r+13:$r+7:$r); 
echo $p;

Mind was blown at the time.
 
PHP:
for($i=9;$r=rand(48,109),$i;--$i)$p.=chr($r>57?$r>83?$r+13:$r+7:$r); 
echo $p;

Mind was blown at the time.
Yep, pretty clever, and it's the same thing here as in Schmide's post.

There are two more variants:
- Print 20 dashes with the given code, without changing it at all, but you are allowed to add some preceding code.
- Print 1 dash with the given code, without changing it at all, but you are allowed to add some preceding code.
 
yep, the trick is in using #defines, can't think of anything else. you don't have to put them before main() though.
The 2nd will work, though no need to loop through all integers:
#define for(X) i=0;
#define printf(X) printf("--------------------");
int i, n = 20;
for (i=0; i < n; i--)
{
printf("-");
}

If you comment out the 2nd #define, you get solution for 1 dash.
 
Back
Top