C implementation of Tic-Tac-Toe in a single call to printf
1–10 of 85 posts
Re: C implementation of Tic-Tac-Toe in a single call to printf
#2Re: C implementation of Tic-Tac-Toe in a single call to printf
#3> Okay, everyone probably knows this. Let's get a bit more advanced.
Ok, but I didn't know about that. What's the use?
Re: C implementation of Tic-Tac-Toe in a single call to printf
#4> %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?
And we did something with it involving string translations. Since we didn't control the translated format string we couldn't just count the characters in the source code.
But it's been a really long time and I don't recall the details.
Re: C implementation of Tic-Tac-Toe in a single call to printf
#5Re: C implementation of Tic-Tac-Toe in a single call to printf
#6Re: C implementation of Tic-Tac-Toe in a single call to printf
#7Some advanced clickbait we have here.
Dennis Ritchie HATES him. Program like a pro with this one weird trick! You won't believe what happens!
That's proper bait.
Re: C implementation of Tic-Tac-Toe in a single call to printf
#8> %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?
Re: C implementation of Tic-Tac-Toe in a single call to printf
#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?
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 price of speed, unless you’ve a compiler that optimizes that away)Re: C implementation of Tic-Tac-Toe in a single call to printf
#10> %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?