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 21 • 6670
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 19:54 UTC — Ed. 2009 Jul 5 at 20:13 UTC
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 17:09 UTC
Down Rodeo
Cap'n Moth of the Firehouse

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

2005 Mar 21 • 6670
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 19:55 UTC — Ed. 2009 Jul 9 at 19:55 UTC
Jake?!

2008 Sep 3 • 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 10 at 04:51 UTC — Ed. 2009 Jul 10 at 04:53 UTC
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 16:24 UTC — Ed. 2009 Jul 10 at 16:36 UTC
SuperJer
Websiteman

2005 Mar 21 • 6670
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 17:48 UTC — Ed. 2009 Jul 10 at 17:48 UTC
the_cloud_system
polly pushy pants

2008 Aug 1 • 3081
-6 ₧
no kudos for you
I drink to forget but I always remember.
 
 
 
2009 Jul 16 at 07:48 UTC
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 17:41 UTC
stanlo
2010 Mar 20 • 1
code
main(int a){for(;a-9;printf("-%d-",a++));}


?
 
 
 
2010 Mar 20 at 01:39 UTC
the_cloud_system
polly pushy pants

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

2005 Mar 21 • 6670
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 21 at 01:50 UTC — Ed. 2010 Mar 21 at 01:55 UTC
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 3 at 02:47 UTC
SuperJer
Websiteman

2005 Mar 21 • 6670
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 24 at 02:04 UTC — Ed. 2012 Apr 24 at 02:04 UTC
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 20:03 UTC
SuperJer
Websiteman

2005 Mar 21 • 6670
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 9 at 00:01 UTC
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 15 at 05:44 UTC
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 20 at 00:27 UTC
chocolate cherry
2015 Aug 19 • 1
Join ussssssss...
 
 
 
2015 Aug 20 at 05:26 UTC
SuperJer
Websiteman

2005 Mar 21 • 6670
I don't think everyone knows coding. But I do.

And you do, right?
 
 
 
2015 Aug 20 at 05:42 UTC
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 20:14 UTC
SuperJer
Websiteman

2005 Mar 21 • 6670
Don't worry. We're all still figuring stuff out.
 
 
 
2015 Aug 25 at 21:04 UTC
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 26 at 05:25 UTC
SuperJer
Websiteman

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