C Puzzle #2! "-1--2--3--4--5--6--7--8-"

C Puzzle #2! "-1--2--3--4--5--6--7--8-"

SuperJer
Websiteman

2005 Mar 20 • 6629
This one I made up myself without ripping anyone off.

The following C program is 43 characters long and prints a particular string.


main(){printf("-1--2--3--4--5--6--7--8-");}



Can you write a program in 42 characters or less which does the same thing?

Assume you can use printf without any includes and ignore all warnings.
 
 
 
2009 Jul 5 at 12:54 PDT — Ed. 2009 Jul 5 at 13:13 PDT
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
Not yet, when you say warnings, presumably this doesn't mean errors? I have one which is 48 characters using loopy loops. Though if we were ignoring one particular error I'd have... *quick check* 47. Ah.
 
 
 
2009 Jul 6 at 10:09 PDT
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
 
 
 
2009 Jul 8 at 13:24 PDT — Ed. 2009 Jul 8 at 13:25 PDT
SuperJer
Websiteman

2005 Mar 20 • 6629
Down Rodeo said:
main(){puts("-1--2--3--4--5--6--7--8-");}



That is a good answer but it doesn't do exactly the same thing as the original program.
 
 
 
2009 Jul 9 at 12:55 PDT — Ed. 2009 Jul 9 at 12:55 PDT
Jake?!

2008 Sep 2 • 198
34 ₧
superjer said:
That is a good answer but it doesn't do exactly the same thing as the original program.


You should be more like the dick judge on American Idol, i.e.:

Your code is sloppy and lacks resonance Down Rodeo, I find myself wondering why I'm reading it at all. Although I was intrigued by it's apparently sassy attitude, it turned out to flat and pretentious. Oh and by the way, IT'S BLATANTLY WRONG AND DOES NOT DO THE EXACT SAME THING YOU PANDY FACKLER. Don't worry though, I'm sure you will find a programming job someday... say 1971..ish.
?!
 
 
 
2009 Jul 9 at 21:51 PDT — Ed. 2009 Jul 9 at 21:53 PDT
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
Yes, helpfully puts() appends a newline. I forgot that :(

main(){int i=1;l:i<9?printf("-%d",i++);goto l;:}

doesn't work I think and is too long anyway.

main(){int i=1;while(i<9)printf("-%d-",i++);}

is shorter, probably works, still too long.

main(){int i=1;for(i;i<9;)printf("-%d-",i++);}

is quite smart I think, ought to work still, the thing with all these is I am hoping since all the bits are on one line the curlies are not needed which is true. Do I get any credit for trying?
 
 
 
2009 Jul 10 at 09:24 PDT — Ed. 2009 Jul 10 at 09:36 PDT
SuperJer
Websiteman

2005 Mar 20 • 6629
Down Rodeo said:
main(){int i=1;while(i<9)printf("-%d-",i++);}

is shorter, probably works, still too long.

That is 45 characters, but it does the right thing...

Down Rodeo said:
main(){int i=1;for(i;i<9;)printf("-%d-",i++);}

is quite smart I think, ought to work still, the thing with all these is I am hoping since all the bits are on one line the curlies are not needed which is true.

You don't need curlies and you really don't need the extraneous i in your for. Change for(i;i<9;) to for(;i<9;) for starters.

Down Rodeo said:
Do I get any credit for trying?

Sure! You get virtual kudos.
 
 
 
2009 Jul 10 at 10:48 PDT — Ed. 2009 Jul 10 at 10:48 PDT
the_cloud_system
polly pushy pants

2008 Aug 1 • 3080
-6 ₧
no kudos for you
I drink to forget but I always remember.
 
 
 
2009 Jul 16 at 00:48 PDT
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
code
main()int i=0;for(;i<9;printf("-%d-",++i));

This is 42! Glory! I really want to try this out but have no compiler.

Of course it doesn't work. AGH
code
main(){int i=0;for(;i<9;printf("-%d-",++i));}

does.
 
 
 
2010 Jan 16 at 09:41 PST
stanlo
2010 Mar 19 • 1
code
main(int a){for(;a-9;printf("-%d-",a++));}


?
 
 
 
2010 Mar 19 at 18:39 PDT
the_cloud_system
polly pushy pants

2008 Aug 1 • 3080
-6 ₧
yah its tech understandable...to a point
I drink to forget but I always remember.
 
 
 
2010 Mar 19 at 20:37 PDT
SuperJer
Websiteman

2005 Mar 20 • 6629
stanlo said:
code
main(int a){for(;a-9;printf("-%d-",a++));}


?


Looks good. You get a cookie!

(It is dependent on argc being exactly 1, though. Not a huge problem but still avoidable.)
 
 
 
2010 Mar 20 at 18:50 PDT — Ed. 2010 Mar 20 at 18:55 PDT
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
Perhaps on a roll?
code
main(){int i=-9;for(;i;printf("%d-",i++))}


I feel this is correct! It's not because it prints the fucker backwards and I'm going to bed :(

See if it involves more fancy bit twiddling, I'll be upset.

Bet it does.

Also, I assume we can't rely on the OS passing us zeroed memory as it typically does? Actually that's only if you malloc, it doesn't care about stack variables, right? Uff.
 
 
 
2012 Apr 2 at 19:47 PDT
SuperJer
Websiteman

2005 Mar 20 • 6629
Down Rodeo said:
Perhaps on a roll?
code
main(){int i=-9;for(;i;printf("%d-",i++))}



How did you get that to compile? You are missing a ; or {} after your for().

malloc is an awfully long name to include. I'd avoid if if possible.

I have a 39 character solution now, btw. No bit twiddling or anything.

You can't rely on anything being zeroed. Unless the C standard says you can. Hint: some types of variables are guaranteed by the standard to be zeroed implicitly (static ones that is).
 
 
 
2012 Apr 23 at 19:04 PDT — Ed. 2012 Apr 23 at 19:04 PDT
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
Yeah, when I said malloc, I meant cmalloc. And you're right, I missed the semicolon because I wasn't trying to compile :p
 
 
 
2012 Apr 24 at 13:03 PDT
SuperJer
Websiteman

2005 Mar 20 • 6629
I just re-found this thread and I'm not sure what 39-character solution I claimed I had but here's one in 38:

code
a;main(){for(;a+8;)printf("%d-",--a);}
 
 
 
2015 Mar 8 at 17:01 PDT
xplinscott
2013 Sep 4 • 1
As you didn't specify how it must be compiled.

code

main(){printf(S);}


code

cc main.c -D S='"-1--2--3--4--5--6--7--8-"' -o main

 
 
 
2015 Aug 14 at 22:44 PDT
xXJigsaw23Xx
IS what she said.

THISISAMEDAL
2007 Oct 21 • 912
41 ₧
Why is it that everyone on this website knows how to do coding?? And where were you guys last semester when I was finishing up my big coding projects for school...
But even then, I had my honor. The largest Banora White tree grew on a wealthy man's estate. It was rumored, that those apples tasted the best, but I never stole from that tree, because the wealthy man's son, was my friend...
 
 
 
2015 Aug 19 at 17:27 PDT
chocolate cherry
2015 Aug 18 • 1
Join ussssssss...
 
 
 
2015 Aug 19 at 22:26 PDT
SuperJer
Websiteman

2005 Mar 20 • 6629
I don't think everyone knows coding. But I do.

And you do, right?
 
 
 
2015 Aug 19 at 22:42 PDT
xXJigsaw23Xx
IS what she said.

THISISAMEDAL
2007 Oct 21 • 912
41 ₧
Well, right now, I'm in my second year of college as a Computer Science major after switching out of animation. So I'm still figuring some stuff out.
But even then, I had my honor. The largest Banora White tree grew on a wealthy man's estate. It was rumored, that those apples tasted the best, but I never stole from that tree, because the wealthy man's son, was my friend...
 
 
 
2015 Aug 25 at 13:14 PDT
SuperJer
Websiteman

2005 Mar 20 • 6629
Don't worry. We're all still figuring stuff out.
 
 
 
2015 Aug 25 at 14:04 PDT
xXJigsaw23Xx
IS what she said.

THISISAMEDAL
2007 Oct 21 • 912
41 ₧
I'm not actually convinced that any programmer is actually sure of what it is that they are doing.
But even then, I had my honor. The largest Banora White tree grew on a wealthy man's estate. It was rumored, that those apples tasted the best, but I never stole from that tree, because the wealthy man's son, was my friend...
 
 
 
2015 Aug 25 at 22:25 PDT
SuperJer
Websiteman

2005 Mar 20 • 6629
Nope. We're all just randomly trying ones and zeros until it comes out sorta right.
 
 
 
2015 Aug 26 at 16:21 PDT
Page [1]