Enable errno if not directly accessed
Do I need to enable errno.h
it even if I don't access errno
directly? For example.
void *mem = malloc(16384);
if (mem == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
I tried a simple piece of code like no inclusion errno.h
and it worked, but I'm not sure if it's okay. Maybe errho.h
also included in other libraries like stdlib.h
and so I don't need to explicitly include it myself?
source to share
You don't need <errno.h>
it if you only use perror()
.
In the Linux Programmers' Guide:
NAME
perror - print a system error message
SYNOPSIS
#include <stdio.h>
void perror(const char *s);
#include <errno.h>
const char *sys_errlist[];
int sys_nerr;
int errno;
This means that you <errno.h>
only need if you are using sys_errlist
, sys_nerr
or errno
. Note that sys_errlist
both sys_nerr
are BSD extensions.
Similar entries can also be found in the C99 standard.
7.19.10.4 The perror function
Summary
#include <stdio.h> void perror(const char *s);
And you <errno.h>
only need it if you use the following:
7.5 Errors
1 The header
<errno.h>
defines several macros, all relevant to error reporting.2 Macros
EDOM EILSEQ ERANGE
which expand to integer constant expressions with type
int
, various positive values and which are suitable for use in#if
preprocessing directives; and
errno
which expands to a modifiable lvalue 175) of a type
int
whose value is specified by a positive error number by several library functions.[...]
4 Additional macros starting with
E
both a digit orE
and an uppercase letter, 177) may also be implementation-specified.
source to share