Fun fact about %n: Mazda cars used to have a bug where they used printf(str) instead of printf("%s", str) and their media system would crash if you tried to play the "99% Invisible" podcast in them. All because the "% In" was parsed as a "%n" with some extra modifiers. https://99percentinvisible.org/episode/the-roman-mars-mazda-...
Lesser known tricks, quirks and features of C
51–60 of 189 posts
Re: Lesser known tricks, quirks and features of C
#52Here's another one. Handy "syntax" that makes it possible to iterate an unsigned type from N-1 to 0. (Normally this is tricky.) for (unsigned int i = N; i --> 0;) printf("%d\n", i); This --> construction also works in JavaScript and so on.
Re: Lesser known tricks, quirks and features of C
#53Be interesting to see when these features showed up. I learned C from the K&R book back in the day and it doesn't mention most of these. Designated initializer is something I'll try to remember, seems handy.
I wish there was some effort to create a modern version while preserving the clarity and conciseness of Kernighan and Ritchie.
Designated initializers in particular are extremely useful. I once halted a factory line for days because of a mistake they would have avoided.
Re: Lesser known tricks, quirks and features of C
#54Fun fact about %n: Mazda cars used to have a bug where they used printf(str) instead of printf("%s", str) and their media system would crash if you tried to play the "99% Invisible" podcast in them. All because the "% In" was parsed as a "%n" with some extra modifiers. https://99percentinvisible.org/episode/the-roman-mars-mazda-...
"format not a string literal" is one warning I always upgrade to an error. Dear reader: you should do this, too!
Re: Lesser known tricks, quirks and features of C
#55I remember once upon a time I thought C was fairly simple, so I decided to write a program to generate ASTs from C programs. I was very wrong and it was kind of a nightmare. There are so many weird little quirks or lesser-used features that I never saw in the wild even in large production codebases; I feel like you really don't _need_ a lot of these features. I can't imagine doing proper compiler work, especially for…
Oh man, I think we all have been this young and naive at some point.
I have spent time working with compilers for this purpose (having realized I did not want to attempt parsing source and generating the AST) and decided it is much easier to let them do the work. That being said, it can still be more than a handful (both GCC and Clang have their eccentricities) and depending on how you are using it you still might be in over your head.
When you start a project like this and end up failing because you simply do not have the depth of knowledge or time to see it to completion it often feels a bit demoralizing from the loss of investment. Truthfully though, having started many such ventures (emulators for 6502 and 80386 to name a few), you get all the benefit of experience from working on a difficult problems without the misery of debugging and model checking until everything until is more/less perfect. It's great fun, you learn a lot, and you should never avoid trying simply because it might be too much to handle.
Re: Lesser known tricks, quirks and features of C
#56What with C also allowing structures as return values, another rarely-used feature, they're really useful for allowing a richer API than the historical `int foo(...)` that so many people are used to seeing.
C has so much legacy that it's really hard for even decades-old (C99!) feature to impose themselves. Or perhaps that's MSVC's lagging support that's to blame :p
Re: Lesser known tricks, quirks and features of C
#57It’s cool to have these, it’s fun to use them for fun. But please don’t use them in production code. Also don’t assume most of them will he known by other developers.
Re: Lesser known tricks, quirks and features of C
#58Earlier quoted context omitted.
The simplest languages tend to be the most difficult. Brainfuck, Binary Lambda Calculus, Unlambda, and other "Turing tarpits" are all extremely difficult to use for anything even mildly complex.
I always felt like unlambda and other SKI calculus esque esolangs(iota comes to mind) could have some kinda strange use case in some kind of generalised genetic programming. It should be possible to create a binary notation for SKI calculus where arbitrary bitstrings will be valid, and so one could randomly mutate and recombine arbitrary programs. Though I've never delved deeper into genetic algorithms and evolutiona…
> It should be possible to create a binary notation for SKI calculus where arbitrary bitstrings will be valid
What if it's not ? How will your genetic petri dish spot and eliminate invalid programs ? > one could randomly mutate and recombine arbitrary programs
What if non-halting programs get generated ?In this vein I've seen magnificent images of 1D cellular automatons that use the surrounding pattern to decide on the local rule for next gen.
Re: Lesser known tricks, quirks and features of C
#59I was a grad when I read it and remember annoying my older coworkers for a few weeks with little gotchas I picked up. "hey what do you think THIS example prints?" "Stop sending me these!"
Re: Lesser known tricks, quirks and features of C
#60> It's possible, because C cares less than more about whitespace
Idiomatically we'd say 'couldn't care less'. I guess we should be glad it wasn't the diabolical and illogical 'could care less'