Live data from Hacker News

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

github.com

51–60 of 85 posts

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

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

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.

This makes me wonder if there's some sort of Unicode equivalent for this?

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

#57

Earlier quoted context omitted.

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.

Hmm that is a good point :-)

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

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

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.

You don’t need Unicode for that. It also requires you use a monospaced font.

I think the feature predates that and Unicode, though. But even then, it fails if you underline text the way it was done at the time, either by using backspace and underline characters or by using termcap (https://en.wikipedia.org/wiki/Termcap)

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

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

The reason he had to use macros is that the actual formatting string is huge and difficult to understand. In this case, macros really serve a useful purpose, instead of just obfuscating code.

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

#60

Earlier quoted context omitted.

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.

If you’re willing to lose almost all portability, I think you could read a value from an I/O port, pass that to printf, use that as the field width for a string to print, count the character length of the resulting string using %n to move it into a variable, and then backspace over that to prevent the output from dirtying your output. You could only use the results in a subsequent call to printf, though.

Of course, just hiding an assignment in a printf argument would be much easier, but wouldn’t be fun.

Of course, that would lose lots and lots of portability, and you could, likely, only get it to work on systems that don’t do memory protection between processes, and then, not all of them.

I guess it would not be very hard to use this trick to have printf read joysticks on many micro’s from the 1980’s.

Post reply on HN