Live data from Hacker News

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

github.com

71–80 of 85 posts

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

#71
post #49

Earlier quoted context omitted.

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.

I understand the temptation. I keep catching myself on writing `unless( ... )` in C++ every couple days, and I've considered adding a macro for it, but ultimately decided against it, as the rest of the team would not understand why I need it.

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

#72

Earlier quoted context omitted.

>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.

Do you not like spending time with your wife? That seems a shame.

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

#73

> %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?

To support format string vulnerabilities! /s

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

#75
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 macros are a form of data compression. Otherwise the string would be absurdly huge

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

#76
post #67

In case you're wondering (like me) how you'd get input from printf(): > We ab^H^Huse [the Turing-completeness of printf()] to implement a the logic of tic-tac-toe entirely within this one printf call (and a call to scanf() to read user input). So it should be "one printf() and one scanf()."

Should be "an infinite number of printf()s and scanf()s".

Would you say that an infinite loop has infinite lines of code?

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

#77

Earlier quoted context omitted.

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

Much of the original Bourne shell was written in C that had been macro'd to look like Algol [1]. E.g. /usr/src/cmd/sh/mac.h: #define IF if( #define THEN ){ #define ELSE } else { #define ELIF } else if ( #define FI ;} #define BEGIN { #define END } #define SWITCH switch( #define IN ){ #define ENDSW } #define FOR for( #define WHILE while( [1] https://research.swtch.com/shmacro

Quite some time ago I saw a piece of C code (probably linked from here) in APL style, I have been trying to find it since, does anyone know what I mean?

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

#78

Earlier quoted context omitted.

Much of the original Bourne shell was written in C that had been macro'd to look like Algol [1]. E.g. /usr/src/cmd/sh/mac.h: #define IF if( #define THEN ){ #define ELSE } else { #define ELIF } else if ( #define FI ;} #define BEGIN { #define END } #define SWITCH switch( #define IN ){ #define ENDSW } #define FOR for( #define WHILE while( [1] https://research.swtch.com/shmacro

Quite some time ago I saw a piece of C code (probably linked from here) in APL style, I have been trying to find it since, does anyone know what I mean?

Might have been this. The style is usually attributed to Arthur Whitney:

https://code.jsoftware.com/wiki/Essays/Incunabulum

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

#79
post #70

Earlier quoted context omitted.

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

"The determined Real Programmer™ can write FORTRAN in any language."

Real Programmers write machine code in any language, thank you very much. (Which TFA rather nearly is, actually, though it's a bit macro-assemblery for a proper example.)

http://www.catb.org/jargon/html/story-of-mel.html

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

#80
post #67

Earlier quoted context omitted.

Should be "an infinite number of printf()s and scanf()s".

Would you say that an infinite loop has infinite lines of code?

It has infinite instructions, yes. (Well, "unbounded" or "endless", if we're being picky.)
Post reply on HN