EmptyData
EmptyData

Reputation: 2586

errno is int or macro in errno.h

I have seen this code:

#if !defined(errno)
extern int errno;
#endif

So my question is whether errno is int or macro , because with #if if can check macro defined or not and after we are doing extern int errno;

in errno.h it is defined like this

#ifdef  _ERRNO_H

/* Declare the `errno' variable, unless it's defined as a macro by
   bits/errno.h.  This is the case in GNU, where it is a per-thread
   variable.  This redeclaration using the macro still works, but it
   will be a function declaration without a prototype and may trigger
   a -Wstrict-prototypes warning.  */
#ifndef errno
extern int errno;
#endif


#endif 

Upvotes: 3

Views: 2279

Answers (2)

Loki Astari
Loki Astari

Reputation: 264699

In C++ (as of n3376) errno is defined as a macro if you include <cerrno> otherwise if you include <errno.h> it is whatever it is defined in C (an int I suspect given the above (but you need to look at the C standard (As per Alok below: "It is unspecified whether errno is a macro or an identifier"))).

n3376:

19.4 Error numbers [errno]

The header <cerrno> is described in Table 43. Its contents are the same as the POSIX header <errno.h>, except that errno shall be defined as a macro.

Upvotes: 2

Karthik T
Karthik T

Reputation: 31972

Check out http://www.cplusplus.com/reference/cerrno/errno/ .It appears the standard defines errno as a macro for c++.

This macro expands to a modifiable lvalue of type int, therefore it can be both read and modified by a program.

In C++, errno is always declared as a macro, but in C it may also be implemented as an int object with external linkage.

cppreference adds some more details with respect to C++11.

errno is a preprocessor macro that expands to a static(until C++11) / thread-local(since C++11) modifiable lvalue of type int.

Upvotes: -1

Related Questions