CTASSERT(9)
π CTASSERT(9) β ok bestie, let's talk compile-time drama
So this is a page from the Kernel Developer's Manual β yes, kernel devs have manuals, and yes, they're iconic. Today's main character is CTASSERT, the compile time assertion macro. Buckle up π
β¨ NAME β¨
CTASSERT β the compile time assertion macro, periodt.
πΈ SYNOPSIS πΈ
#include <sys/param.h>
#include <sys/systm.h>
CTASSERT(expression);
That's it. Two headers and a vibe. π¦
π DESCRIPTION π
Okay so first thing first, and I need you to hear me on this one: the CTASSERT() macro is deprecated. Like, it still works, but it's giving "my older cousin's MySpace". The C11 standard's _Static_assert() is the one you should be using instead, fr fr.
And if you're on a compiler that predates C11? Don't panic, bestie π«Ά β you should include the sys/cdefs.h header, because that's the one that provides compatibility for pre-C11 compilers. Cute of them honestly.
Now for what it actually does: the CTASSERT() macro evaluates expression at compile time, and if that expression is false, it causes a compiler error. No runtime surprises, no "oops we found out at 3am" β the compiler just tells you straight up. It's giving honest queen energy. π
And why would you want that? Because CTASSERT() is useful for asserting the size or alignment of important data structures and variables during compilation β stuff that would otherwise cause the code to fail at run time. Which is so much worse. Run time failures are the worst kind of plot twist. π
π₯ EXAMPLES π₯
Say you wanna assert that the size of the uuid structure is 16 bytes. You write:
CTASSERT(sizeof(struct uuid) == 16);
And the compiler either agrees with you or it does not. No in-between. No cap. β¨
π SEE ALSO π
KASSERT(9)β the runtime sibling, go say hi π
π AUTHORS π
This manual page was written by Hiten M. Pandya <hmp@FreeBSD.org>. We love a credited author. π
And that's the whole page, bestie! This little gem came to you from Debian, dated August 1, 2015 β so yeah, it's been slaying since 2015. Go assert something at compile time, your future self at 3am will thank you. Mwah π πβ¨
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 2003 Hiten M. Pandya
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms