errno是errno.h中的int或macro

时间:2013-01-28 06:07:40

标签: c++

我看过这段代码:

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

所以我的问题是errno是int还是宏,因为使用#if if是否可以检查宏定义与我们在extern int errno;

之后

在errno.h中,它的定义如下

#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 

2 个答案:

答案 0 :(得分:2)

在C ++中(截至n3376),如果包含< cerrno>,则将errno定义为宏。否则,如果你包括< errno.h>它是在C中定义的任何内容(我怀疑上面给出了一个int(但是你需要查看C标准(按照下面的Alok:“未指定errno是宏还是标识符”))。 / p>

n3376:

19.4错误号码[错误号]

  

标题< cerrno>表43中描述了它的内容。它的内容与POSIX标题< errno.h>相同,除了errno应被定义为宏。

答案 1 :(得分:-1)

签出http://www.cplusplus.com/reference/cerrno/errno/。看来标准将errno定义为c ++的宏。

  

此宏扩展为int类型的可修改左值,因此它可以由程序读取和修改。

     

在C ++中,errno总是声明为宏,但在C中它也可以实现为具有外部链接的int对象。

cppreference增加了有关C ++ 11的更多细节。

  

errno是一个预处理器宏,它扩展为静态(直到C ++ 11)/线程局部(自C ++ 11)可修改的int类型左值。

相关问题