3

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 
EmptyData
  • 2,386
  • 2
  • 26
  • 42

2 Answers2

2

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.

Community
  • 1
  • 1
Martin York
  • 257,169
  • 86
  • 333
  • 562
  • 1
    +1 It depends on what one includes, `` or ``. It would be good if you added more detail on whats the [difference between including `` and ``](http://stackoverflow.com/questions/13889467/should-i-include-xxxx-h-or-cxxxx-in-c-programs). – Alok Save Jan 28 '13 at 06:19
  • In C, It is unspecified whether `errno` is a macro or an identifier declared with external linkage. – Alok Save Jan 28 '13 at 06:22
  • @AlokSave: The 2011 C standard no longer has that wording, but it still says that "If a macro definition is suppressed in order to access an actual object, or a program defines an identifier with the name **errno**, the behavior is undefined." – Keith Thompson Jan 28 '13 at 08:03
-1

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.

Community
  • 1
  • 1
Karthik T
  • 31,456
  • 5
  • 68
  • 87
  • 2
    Quoting a site that is well known to be riddled by mistakes is not really a good idea. – Martin York Jan 28 '13 at 06:18
  • @LokiAstari cplusplus? I was not aware of this reputation, only that it doesnt seem to be updated since C++11. – Karthik T Jan 28 '13 at 06:19
  • For some reason beyond comprehension, moderators deleted [the canonical Q&A explaining this](http://programmers.stackexchange.com/questions/88241/whats-wrong-with-cplusplus-com). Presumably they were not moderators who are experts in C++ and therefore did not understand the damage that this website does. There is a stub [here](http://stackoverflow.com/questions/11972076/why-is-the-cplusplus-website-bad), and an archive [here](http://stackroulette.com/programmers/88241/undefined). – Lightness Races in Orbit Jan 28 '13 at 06:23
  • Prefer http://cppreference.com and make sure please that when you provide links to explanations of C++ it's either a link to a list of peer-reviewed books, or to http://cppreference.com! – Lightness Races in Orbit Jan 28 '13 at 06:23
  • As an aside, citing some site's claims about the standard is using a _secondary_ source. This is okay but you'd get an upvote from me if you quoted the standard itself. – Lightness Races in Orbit Jan 28 '13 at 06:24
  • @Non-StopTimeTravel Thanks for the references! – Karthik T Jan 28 '13 at 06:24
  • @Non-StopTimeTravel: I remember the good old cplusplus blunders thread. It started here and was migrated to programmers i suppose. It does not exist anymore? why so? – Alok Save Jan 28 '13 at 06:25
  • @Non-StopTimeTravel That brings me to something else that puzzles me. Is the actual C++ standard available online for free? All I could find is that the current standard needs to be purchased from ISO and that draft versions can be downloaded. – Karthik T Jan 28 '13 at 06:26
  • @KarthikT: That is correct. Like everything worthwhile in life, the C++ standard text is not financially free. – Lightness Races in Orbit Jan 28 '13 at 06:26
  • You can always find the draft versions of the standard available for free which are nearly same as the final standard except for the covering page which say draft and not approved standard. – Alok Save Jan 28 '13 at 06:28