assert(3)

๐Ÿ’… assert(3) โ€” bestie's guide to yelling when things aren't true ๐Ÿ’…

okay so real talk, this is assert(3) from the Standard C library (libc, -lc). it lives in the Library Functions Manual and it is genuinely one of the most iconic little macros in all of C, no cap. ๐ŸŽ€


โœจ NAME โœจ

assert โ€” abort the program if assertion is false. that's it. that's the whole vibe. it checks a thing, and if the thing is a lie, your program gets absolutely ended. ๐Ÿ’€


๐ŸŒธ LIBRARY ๐ŸŒธ

Standard C library (libc, -lc) ๐Ÿซถ


๐Ÿฆ‹ SYNOPSIS ๐Ÿฆ‹

#include <assert.h>

void assert(bool expression);

๐Ÿ‘‘ DESCRIPTION ๐Ÿ‘‘

so here's the tea, bestie: this macro helps programmers find bugs in their programs, or handle exceptional cases via a crash that will produce limited debugging output. it's like a little bodyguard for your assumptions. ๐Ÿ’–

if expression is false (i.e., compares equal to zero ๐Ÿ˜ญ), assert() prints an error message to standard error and terminates the program by calling abort(3). the error message includes the name of the file and function containing the assert() call, the source code line number of the call, and the text of the argument. something like:

prog: some_file.c:16: some_func: Assertion `val == 0' failed.

like, it literally snitches on exactly where and why and shows you the receipts. ๐Ÿ”ฅ

BIG ONE, pay attention ๐Ÿšจ: if the macro NDEBUG is defined at the moment <assert.h> was last included, the macro assert() generates no code, and hence does nothing at all. poof. gone. no thoughts, head empty. it is not recommended to define NDEBUG if using assert() to detect error conditions, since the software may behave non-deterministically. so like, don't do that. ๐Ÿ™ˆ

warning bestie: if you're using assert() to catch real errors and you define NDEBUG, your software may behave non-deterministically. that is not the slay you think it is.

โญ RETURN VALUE โญ

No value is returned. it returns nothing. void. the void is the moment. โœจ


๐Ÿงš ATTRIBUTES ๐Ÿงš

for an explanation of the terms used in this section, see attributes(7). ๐ŸŒˆ

  • Interface: assert()
  • Attribute: Thread safety
  • Value: MT-Safe

so yeah, she's MT-Safe. safe in multithreaded situations, periodt. ๐Ÿ‘‘


๐Ÿ’œ STANDARDS ๐Ÿ’œ

C11, POSIX.1-2008. certified. official. we stan an internationally standardized macro. ๐Ÿ“œโœจ


๐ŸŒธ HISTORY ๐ŸŒธ

C89, C99, POSIX.1-2001.

and here's a little lore drop for you ๐Ÿ•ฐ๏ธ: in C89, expression is required to be of type int and undefined behavior results if it is not, but in C99 it may have any scalar type. glow-up fr fr. ๐Ÿ’…


๐Ÿ’€ BUGS ๐Ÿ’€

okay so this one is important. assert() is implemented as a macro; if the expression tested has side-effects, program behavior will be different depending on whether NDEBUG is defined. this may create Heisenbugs which go away when debugging is turned on. ๐Ÿ˜ญ

translation: if your expression does stuff (not just checks stuff) and you flip NDEBUG on or off, your program will legit behave differently. and you'll get one of those cursed bugs that vanishes the second you go looking for it. Heisenbugs. absolutely unhinged behavior. do not put side effects in your assertions, bestie. ๐Ÿซถ

๐ŸŒˆ SEE ALSO ๐ŸŒˆ


and that's the whole page, angel ๐Ÿ’– go forth and assert your truth (but like, only side-effect-free truths, okay?) ๐ŸŒŸ

this lil masterpiece is from Linux man-pages 6.20, dated 2026-02-08. mwah, xoxo, go read abort(3) next, iykyk. ๐ŸŽ€๐Ÿ’…

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: Linux-man-pages-copyleft
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms