Live data from Hacker News

Lesser known tricks, quirks and features of C

blog.joren.ga

21–30 of 189 posts

Re: Lesser known tricks, quirks and features of C

#21
post #6

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

Fun fact about %n:

The %n functionality also makes printf accidentally Turing-complete even with a well-formed set of arguments. A game of tic-tac-toe written in the format string is a winner of the 27th IOCCC.

- sez wiki.

A not so fun fact:

Because the %n format is inherently insecure, it's disabled by default.

- MSVC reference.

Re: Lesser known tricks, quirks and features of C

#22
post #3

Yet another proof that C is simple but not easy.

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.

Forth. It’s easy for a small cute script. But quickly becomes a PITA.

Re: Lesser known tricks, quirks and features of C

#24

> The 0 width field tells that the following bit fields should be set on the next atomic entity (char). This isn't correct since int can't be less than 16-bits. Fields are placed on the nearest natural alignment for the target platform, which might not support unaligned access.

I think I'll use other example. Thanks!

Re: Lesser known tricks, quirks and features of C

#26
post #3

Yet another proof that C is simple but not easy.

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 evolutionary programming, my sense is that genetic algorithms tend to be restricted to parameterised algorithms where the "genes" determine the various parameters. Which can be great for optimisation problems.

It's one of those weird ideas I've had kicking about for years but never did anything about, and yet I keep coming back to it.

Re: Lesser known tricks, quirks and features of C

#27
post #4

Nice article. Saw a few things I wish I'd known about. 1. %n in printf would be handy when writing CLIs dealing w/ multiple lines or precise counts of backspaces. 2. Using enums as a form of static_assert() is a great idea (triggering a div by zero compiler error).

using enums as a form of static_assert is very bad when C nowadays literally has static_assert (_Static_assert: https://gcc.godbolt.org/z/bfv6rKdKM)

Re: Lesser known tricks, quirks and features of C

#28
post #13

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

AFAICT this would parse as "(i--) > 0", there's no "-->" operator.

https://stackoverflow.com/questions/1642028/what-is-the-oper...

Re: Lesser known tricks, quirks and features of C

#29

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

I was hesitant to put it on the list, but fine, you convinced me
Post reply on HN