Defer Available in GCC and Clang

C gets a cleanup button — devs cheer while C++ diehards say “just switch”

TLDR: Clang now supports a new “defer” feature that auto-runs cleanup code, with a formal spec moving toward standardization. The community is split between excitement for fewer bugs and a chorus of “just use C++,” while pragmatists share tips and cheer a simpler, safer C—no deep magic required.

C’s new toy just dropped: a “defer” keyword that tells your code, “clean up when we’re done.” It’s already usable in Clang 22, with a formal spec (TS 25755) inching through ISO’s paperwork maze. Fans say this kills the classic C headache—forgotten frees, stuck locks, and spaghetti exits. One commenter called it “effectively Go’s defer with extra braces,” and the vibe is: finally.

But of course, no C party is complete without a language war. The C vs. C++ cage match broke out instantly. One camp insists “just use C++ destructors,” the feature that auto-cleans when objects go out of scope. Others clap back: not everyone can switch languages or deal with C++ baggage; a simple cleanup button in C is a win. Another thread went full pedant-in-chief, reminding folks to place defer right after allocation—then winking that freeing a null pointer is actually safe.

There’s also meta-drama over the fallback magic for GCC (older versions use a controversial feature that sparked “flame wars”), with the author swearing there’s no sneaky code or security trapdoors. Meanwhile, cheerleaders link this hype blog and chant “go, go, go” as C finally gets a little modern polish.

Key Points

  • TS 25755 defining C’s defer feature is complete and moving through ISO publication.
  • Clang supports defer starting with version 22; GCC integration is in progress.
  • A portability approach uses stddefer.h when available, or a macro-based fallback for GCC > 8 and Clang 22+.
  • The GCC fallback leverages nested functions and is stated to avoid hidden functions and trampolines.
  • Examples show using defer for freeing allocated memory and unlocking mutexes; braces are required for the GCC fallback.

Hottest takes

“effectively golang’s `defer`… good addition” — kjgkjhfkjf
“Anyone who writes C should consider using C++ instead” — Panzerschrek
“every time I use it I wish I could just ‘defer’ instead” — LexiMax
Made with <3 by @siedrix and @shesho from CDMX. Powered by Forge&Hive.