Name

errno macro — Global error code object

Synopsis

int& errno
               

The errno macro expands into an int lvalue. Standard library functions can store an error code in errno and return an error status to the caller. You can also store a value in errno (e.g., resetting the error code to 0).

When a program starts, errno is initially 0. No library function resets errno to 0. Any library function might set errno to a nonzero value, even if it is not documented to do so. Therefore, the only time it is safe to check errno is after a library function returns an error status and is documented to set errno in that case.

image with no caption

The C++ standard is not explicit as to whether errno is truly a macro (versus a variable). The intent of the standard committee is to define errno as a macro, so do not use std::errno, and if your library declares errno as a variable in the std:: namespace, you can define your own macro:

#define errno (::std::errno)

In a multithreaded environment, a library implementation typically ensures that each thread gets a separate copy of errno. Such considerations fall outside the realm of the C++ standard. Consult your compiler and library documentation for details.

See Also

perror in <cstdio> , strerror in <cstring>

Get C++ In a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.