C-libs 0.1.0
Some C utils libraries
 
Loading...
Searching...
No Matches
assert.h File Reference

Header file for assertions. More...

#include <stdio.h>
#include <stdlib.h>

Macros

#define COMPILE_ASSERT(predicate, file)
 Asserts a compile-time condition.
 
#define ASSERT(predicate, fmt, ...)
 Asserts a runtime condition with a formatted message.
 
#define assert(predicate)
 Asserts a runtime condition with a default message.
 

Detailed Description

Header file for assertions.

This header provides macros for creating compile-time and runtime assertions. Compile-time assertions are useful for validating conditions known during compilation (such as sizeof) and stop the compilation with an error if the conditions are not met.

Note
You can define NOASSERT to remove assertions.

Macro Definition Documentation

◆ ASSERT

#define ASSERT ( predicate,
fmt,
... )
Value:
do { \
if (!(predicate)) { \
fprintf(stderr, \
"\033[0;31mAssertion failed\033[0m: `%s`, with " \
"message `" fmt "` (%s:%d in %s())\n", \
#predicate, __VA_ARGS__, __FILE__, __LINE__, \
__func__); \
abort(); \
} \
} while (0)

Asserts a runtime condition with a formatted message.

This macro checks a runtime condition (predicate). If the condition is false, it prints an error message to stderr with details about the failed assertion, including the file name, line number, and function name, and then aborts the program. The error message is formatted according to the provided format string and arguments.

Parameters
predicateThe condition to assert. Must be a runtime expression.
fmtThe format string for the error message.
...Variadic arguments for the format string.

Example usage:

int value = 10;
ASSERT(value == 10, "Value should be %d", 10);
// No output, as the condition is true.
ASSERT(value == 0, "Value should not be %d", value);
// Outputs an assertion failure message and aborts.
#define ASSERT(predicate, fmt,...)
Asserts a runtime condition with a formatted message.
Definition assert.h:76
Examples
example_assert.c.

◆ assert

#define assert ( predicate)
Value:
ASSERT(predicate, "%s", "failed")

Asserts a runtime condition with a default message.

This macro checks a runtime condition (predicate). If the condition is false, it prints an error message to stderr with details about the failed assertion, including the file name, line number, and function name, and then aborts the program. The error message is set to "failed".

Parameters
predicateThe condition to assert. Must be a runtime expression.

Example usage:

int value = 10;
assert(value == 10); // No output, as the condition is true.
assert(value == 0); // Outputs an assertion failure message and aborts.
#define assert(predicate)
Asserts a runtime condition with a default message.
Definition assert.h:114
Note
This macro is only defined if assert is not already defined and is an alias to the ASSERT() macro with "failed" as default message.
Defining NOASSERT will remove the ASSERT block and thus the assert also

◆ COMPILE_ASSERT

#define COMPILE_ASSERT ( predicate,
file )
Value:
__CASSERT_IMPL(predicate, file, __LINE__)

Asserts a compile-time condition.

This macro asserts a compile-time condition. If the condition is false, it generates a compilation error.

Parameters
predicateThe condition to assert. Must be a compile-time constant.
fileA unique identifier for the assertion, typically the file name.
Note
You can define NOASSERT to remove the COMPILE_ASSERT block.

Example usage:

COMPILE_ASSERT(sizeof(int) == 4, int_size_check);
// Asserts that int is 4 bytes
#define COMPILE_ASSERT(predicate, file)
Asserts a compile-time condition.
Definition assert.h:50
Examples
example_assert.c.