CKTIS / C
errno — C23 error macro
`errno` from `<errno.h>` is a macro expanding to a modifiable lvalue of type `int` with thread storage duration. It is neither a function nor a constant: use `errno`, never `errno()`, and every thread has its own object.
Version: C23
Syntax
errnoerrno from <errno.h> is a macro expanding to a modifiable lvalue of type int with thread storage duration. It is neither a function nor a constant: use errno, never errno(), and every thread has its own object.
The example sets errno = EDOM in the main thread while a worker sets its own errno = ERANGE. Output 1 1 confirms the worker value and that its write did not change the main thread’s value.
The initial thread starts with zero, but the initial representation in other threads is indeterminate; no library function ever sets errno to zero. Before a documented call set it to zero, inspect it only when the result signals failure, and save it immediately. Suppressing the macro to access an object or defining an identifier named errno is UB. The EDOM, ERANGE, and EILSEQ macros are distinct positive integer constants of type int, but their numbers are implementation-defined. They denote a domain error, a range error, and an invalid multibyte-sequence error respectively; POSIX values such as EINTR are outside ISO C core.
Examples
#include <errno.h>
#include <stdio.h>
#include <threads.h>
static int worker(void *unused) {
(void)unused;
errno = ERANGE;
return errno == ERANGE ? 0 : 1;
}
int main(void) {
errno = EDOM;
thrd_t thread;
if (thrd_create(&thread, worker, NULL) != thrd_success) return 1;
int result;
if (thrd_join(thread, &result) != thrd_success) return 1;
printf("%d %d\n", result == 0, errno == EDOM);
return 0;
}
Result
1 1
The example sets `errno = EDOM` in the main thread while a worker sets its own `errno = ERANGE`. Output `1 1` confirms the worker value and that its write did not change the main thread’s value.