Live data from Hacker News

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

github.com

41–50 of 85 posts

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

#41
post #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.

printf once in a while

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

#42

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. Maybe we could call this additional 'holiday pay' or a longer 'weekend' and mandate it through law so that everyone benefits.

Ha, if I had a longer weekend my wife would demand I use it taking her out.

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

#43
post #35

Oddly enough I find that the #define macro soup detracts from the performance here. I was rather unimpressed at first because obfuscating C code by just #define'ing a bunch of code is a trivial and rather uninteresting way to write unreadable code. Of course you can make any arbitrary code look like a printf call with enough macros! But it's actually a lot more clever than that. I feel like this would be a lot more i…

One time, I had to maintain C code That somebody had #define'd to look like FORTRAN. It was impressive.

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

#46

Some advanced clickbait we have here.

Well, I got downvoted. I should have mentioned that I meant this in a good way. My account is new and I have since read the rules in the welcome page. But think about it, a whole game implemented only in a print statement? Wow, that can’t be real. And it is at the very 1st place of the top posts. I clicked on it expecting a cool article explaining something interesting, but no. It is the implementation. So, well done.

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

#47
post #36

Earlier quoted context omitted.

On line 57: https://github.com/carlini/printf-tac-toe/blob/master/printt...

Ah, well spotted, thanks. I didn't think it was fair to call the title click bait just because it used a while loop to drive the calls to printf, but also including a scanf call is definitely not in the same spirit.

Alas, it's technically difficult to get input from the user using printf.

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

#48
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…

This only works if you're not dealing with Unicode, where the number of bytes, the number of characters, and the width of those characters can all vary.

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

#49
post #35

Oddly enough I find that the #define macro soup detracts from the performance here. I was rather unimpressed at first because obfuscating C code by just #define'ing a bunch of code is a trivial and rather uninteresting way to write unreadable code. Of course you can make any arbitrary code look like a printf call with enough macros! But it's actually a lot more clever than that. I feel like this would be a lot more i…

One time, I had to maintain C code That somebody had #define'd to look like FORTRAN. It was impressive.

I've had to deal with code like that, only poorly mimicking Lisp instead: `#define unless(_x) if (!(_x))` etc...

One of my favourite accidental features in Rust is that macros are so annoying and cryptic to write that people think twice before abusing them.

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

#50
post #35

Oddly enough I find that the #define macro soup detracts from the performance here. I was rather unimpressed at first because obfuscating C code by just #define'ing a bunch of code is a trivial and rather uninteresting way to write unreadable code. Of course you can make any arbitrary code look like a printf call with enough macros! But it's actually a lot more clever than that. I feel like this would be a lot more i…

If you are curious, this is what the resulting format string looks like after expanding all the macros:

https://gist.github.com/hugomg/73e91ebca7ced3c5c6f955e9e830b...

Post reply on HN