Live data from Hacker News

C implementation of Tic-Tac-Toe in a single call to printf

github.com

21–30 of 85 posts

Re: C implementation of Tic-Tac-Toe in a single call to printf

#22

This is just crazy and I love it From a description on the author's website[1] of a Doom clone written in 13k of JavaScript. > Until recently all content on this website was research, and while writing papers can be fun, sometimes you just need to blow off a little steam. I think more companies should allow for their employees to have some plain old fun with no strings attached on a regular basis. [1] https://nichola…

> I think more companies should allow for their employees to have some plain old fun with no strings attached on a regular basis.

Nicholas Carlini's resume is crazy-insane and he's highly desirable, so of course Google Brain is going to lend him far more flexibility than Google Analytics would have with a recent college-hire, or at the other end of the supply/demand distribution: the working flexibility Amazon would afford a warehouse employee (if they aren't a contractor...).

Re: C implementation of Tic-Tac-Toe in a single call to printf

#23
post #9

> %n takes a pointer and writes (!!) the number of bytes printed so far. > Okay, everyone probably knows this. Let's get a bit more advanced. Ok, but I didn't know about that. What's the use?

One use is aligning outputs: char *prefix = "example"; char *line1 = "line 1"; char *line2 = "line 2"; printf("%s: %n%s\n", prefix, &n, line1); printf("%*s%s\n", n, "", line2); will output example: line 1 line 2 That’s a bit more robust than using strlen(s)+2 , where you have to keep that magic constant 2 in sync with ": ". Moving ": " to a variable and using strlen(s)+strlen(separator) would fix that, though (at the…

> That’s a bit more robust than using strlen(s)+2

strlen wouldn't even be an option if you were formatting something that's not a string e.g.

    int n;
    int prefix_num = 23;
    char *line1 = "line 1";
    char *line2 = "line 2";
    printf("example %d: %n%s\n", prefix_num, &n, line1);
    printf("%*s%s\n", n, "", line2);

Re: C implementation of Tic-Tac-Toe in a single call to printf

#26
post #15
post #11

I am afraid that the author has disqualified himself by publishing the source code before the judging of IOCCC 2020 is over. See catch 22 in the rules: "The judges STRONGLY prefer to not know who is submitting entries to the IOCCC."[1] Even the winners are asked not to publish the code until it is available on the IOCCC web site.[2] [1] https://ioccc.org/2020/rules.txt [2] Personal communication

‘Strongly prefer‘ != disqualification

'Strongly prefer'== banned for life

Re: C implementation of Tic-Tac-Toe in a single call to printf

#27
post #16
post #7

Earlier quoted context omitted.

Nah. Dennis Ritchie HATES him. Program like a pro with this one weird trick! You won't believe what happens! That's proper bait.

Hence advanced bait. printf() is called in a while loop, so it is hardly a single call.

I don’t want to jump on the title hate bandwagon - what the author has done is definitely creative and clever and this discussion detracts from it - but it also uses scanf.

Re: C implementation of Tic-Tac-Toe in a single call to printf

#28

> in a single call to printf > while(*d) printf(fmt, arg) How's that a single call ?

> How's that a single call

syntactically it is. But it's not a very clear description. So how would you describe that single call location getting executed in a loop so everybody understands what is really meant? A single line of code is worse.

Re: C implementation of Tic-Tac-Toe in a single call to printf

#29

Love the attempt the blindside the reader with funky formatting showing "%N".

That's not too unusual for IOCCC entries. Of the top of my head, there's a flight simulator shaped like an airplane, and an addition routine shaped like a full-adder.

A more subtle touch is that the #defines spell out NOUGHTs AnD CRoSSES.

Re: C implementation of Tic-Tac-Toe in a single call to printf

#30
post #13
post #9

Earlier quoted context omitted.

One use is aligning outputs: char *prefix = "example"; char *line1 = "line 1"; char *line2 = "line 2"; printf("%s: %n%s\n", prefix, &n, line1); printf("%*s%s\n", n, "", line2); will output example: line 1 line 2 That’s a bit more robust than using strlen(s)+2 , where you have to keep that magic constant 2 in sync with ": ". Moving ": " to a variable and using strlen(s)+strlen(separator) would fix that, though (at the…

I'd never heard of %n, but I use printf's return value (the number of bytes written) for this kind of purpose, so n = printf("%s: ", prefix); printf("%s\n", line1); printf("%*s%s\n", n, " ", line2);

Yeah but the difference is that you can use %n wherever you need it in the format string. Depending on what arguments come after it figuring out the length of the printed arguments might not be trivial whereas with printf, it already has to keep a count of it during execution in order to return it at the end so it's easy to add support for it.
Post reply on HN