💾 Archived View for michal_atlas.srht.site › posts › $.gmi captured on 2024-08-31 at 12:18:38. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2024-02-05)

-=-=-=-=-=-=-

This code is a completely valid C source file:

$($){
  return $
;}

main () {
  printf("%d\n", $(23))
;}

and I don’t even mean that it’s part of a project or whatever, this verbatim compiles in both gcc and clang!!! Go ahead try it, I’ll wait.

Okay, the warnings spoil a bit of the fun, it’s still interesting though, I wondered what are valid characters in an identifier, since I was kinda let down by the fact that you have the entirety of UNICODE at your disposal but not most of ASCII. So I tried all the ASCII characters, interestingly enough NULL didn’t pose a problem, the compiler simply ignored it, and everything else was either ignored or threw an error as expected, except $.

$ for some reason works, GCC doesn’t even give you a warning (Although that’s not its job, that should be a linting thing). The next part is that everything in C is assumed to be int unless you know otherwise, so printf even though undeclared just happens to fit the signature it links against, and doesn’t cause problems. Arguments seem to simply be bound in a new scope after the function, which is logical now that I say it out loud, but allows us to just shadow the function $ with the argument $, and what’s the type of $? Since it’s undeclared it’s just int again, same for both the variable and function.

That’s it, it’s not really too interesting but it’s something I discovered a long time ago, and find quite amusing and keep showing around.