Weekend projects: getting silly with C
Switch tricks, Duff’s device, and control‑flow abuse
- Several comments connect the article’s “switch shenanigans” to Duff’s device: exploiting fallthrough and the ability to interleave
switchwith loops to unroll code. - Others argue the article’s tricks differ: more about removing angle brackets and pushing control keywords left, whereas Duff’s device is specifically a loop‑unrolling pattern.
- Some see these patterns as clever but dangerously unclear, preferring explicit
gotoover abusingswitch.
Memory‑mapped I/O, volatile, and intrinsics
- One line of discussion criticizes treating MMIO as normal memory plus
volatile, calling it a type‑system hack and a layering violation. - The alternative proposed is platform‑specific intrinsics for MMIO operations, making constraints explicit (alignment, bit‑level writes).
- Pushback: others see
volatileand evenregisteras acceptable, with implementation‑defined behavior being natural in this domain; they consider atomics intrinsics more problematic in practice.
C learning, style, and tooling
- Multiple commenters value learning C for its closeness to hardware and as a foundation for understanding abstractions.
- Modern compilers and static analysis are said to mitigate many classic footguns (e.g., assignments in
ifconditions). - Debate around “Yoda conditions” (
if (1 == x)) versus relying on warnings; some consider such defensive style unnecessary and less readable. - Comparison with Python: students gravitate to high‑level tools but may not understand underlying computation or statistics, leading to fragile code.
Undefined behavior and “time travel”
- Long subthread on whether UB can affect behavior before the UB point (“time‑traveling UB”).
- One side asserts C’s model (especially in C23) requires observable behavior before UB to remain as specified; UB cannot “go back in time.”
- Others note that compilers assume absence of UB and may optimize aggressively, sometimes appearing to remove or reorder side effects; examples with divisions by zero and
volatileare discussed. - There is disagreement over how strictly compilers conform and whether some optimizations violate the intended guarantees.
Compiler extensions and oddities
- GNU/Clang extensions highlighted:
case 1 ... 10:ranges, computed gotos, ternary without middle expression. - Unusual but standard C behaviors:
4[arr]==arr[4]; lifetime rules allowinggototo a label that uses a variable declared later in the block. - Switch‑based macros for coroutines and for auto‑breaking cases are mentioned as real‑world but niche techniques.
Obfuscation vs everyday practice
- Several commenters stress that the most extreme examples (including IOCCC‑style code) are not used in normal C development.
- Real “C wizardry” is seen more in managing overflow, concurrency, architecture, and build systems than in syntactic tricks.