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

char format[10] = "%10.3f%10.4f%10.3f%10.4f%10.3f%10.4f%4d";

printf( format, x, y, a, b, c, d, n );

You can get similar behavior from C, reusing the format specification? No idea if this was true in the K&R days though.



But not the repetition inside of one specification which I illustrated in the example (note the 3 before the braces in the 2 FORMAT), and much more important, no compile-time type checking and compile time code generation for output. In C it's a library call and most of the processing happens at the run-time. And yes I know of gcc checks.

Still whoever uses some printf variant today, misses some aspects of what was working in 1956.


I was only addressing your first point, not your second example which you demonstrated clearly earlier. The recursive syntax is impressive alright - thanks for sharing it. I was unaware of it, as I'm sure many others were. I was not suggesting C has a built in method for achieving the results of the second example. I'm not certain about the compile time versus execution time distinctions. The compiler behaviour could depend on the mutability of the objects. It's possible a C #define constant would cause different compile time behaviour to variables. I'm not a compiler expert in either language so I can't provide any insight in any case.

Those GCC checks you mentioned are handy and would have avoided a bug in production code a colleague encountered a few years ago, but I imagine they are more recent than C in 1982, and certainly more recent than Fortran in 1956

#define formata "%10.3f%10.4f"

#define formatb(z) z z z "%4d\n"

printf( formatb(formata), x, y, a, b, c, d, n );

The example you've given is thought provoking!


Neat, I didn't know Fortran did printf right.

OCaml and Rust both have compile-time format string checking and code generation as well.




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

Search: