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.
C implementation of Tic-Tac-Toe in a single call to printf
51–60 of 85 posts
Re: C implementation of Tic-Tac-Toe in a single call to printf
#52Title correction: Tic Tac Toe written almost entirely in #defines.
Re: C implementation of Tic-Tac-Toe in a single call to printf
#53Re: C implementation of Tic-Tac-Toe in a single call to printf
#54Re: C implementation of Tic-Tac-Toe in a single call to printf
#55- esoteric modes of operation of a common function
- truly novel use of macros
- visibly beautiful
Re: C implementation of Tic-Tac-Toe in a single call to printf
#56Aaargghhh MSVC 2019 doesn't like this :(
Re: C implementation of Tic-Tac-Toe in a single call to printf
#57Earlier 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.
Re: C implementation of Tic-Tac-Toe in a single call to printf
#58Earlier 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.
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
#59Oddly 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…
Re: C implementation of Tic-Tac-Toe in a single call to printf
#60Earlier 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.
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.