Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Some advanced clickbait we have here.


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.


Where?

    int main() {
        while(*d) printf(fmt, arg);
    }



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.


In that case it might just be easier to ROP your way to scanf.


Hmm that is a good point :-)


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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: