在C中是“unix”限制关键字?

is “unix” restricted keyword in C?

这个代码在gcc版本4.3.2(Debian 4.3.2-1.1)上不能为我编译

1
2
3
main(){
  int unix;
}

我检查了C关键字列表,"unix"不是其中之一。
为什么我收到以下错误?

1
unix.c:2: error: expected identifier or ‘(’ before numeric constant

任何人?


unix不是标准保留的标识符。

如果使用-std=c89-std=c99进行编译,gcc编译器将按预期接受该程序。

从gcc手册(https://gcc.gnu.org/onlinedocs/cpp/System-specific-Predefined-Macros.html),重点是我的。

... However,
historically system-specific macros
have had names with no special prefix;
for instance, it is common to find
unix defined on Unix systems. For all
such macros, GCC provides a parallel
macro with two underscores added at
the beginning and the end. If unix is
defined, __unix__ will be defined too.
There will never be more than two
underscores; the parallel of _mips is
__mips__.


unix是预处理器在gcc中使用的定义之一
获取defs使用列表

1
gcc -dM -E  -x c /dev/null

(-dM告诉gcc调试转储defs -E告诉它在预处理后停止并且-x c / dev / null告诉他假装/ dev / null是一个c文件)


通过预处理器运行代码以找出编译器实际看到的内容:

1
gcc -E unix.c

然后查看预处理器是保留还是转换了变量unix


它不是关键字。

它是用于标识系统类型的预定义宏。在Unix和Unix之类的系统上,它被定义为1

要禁用此功能,请使用-ansi选项:

In C mode, this is equivalent to -std=c89. In C++ mode, it is equivalent to -std=c++98.
This turns off certain features of GCC that are incompatible with ISO C90 (when compiling C code), or of standard C++ (when compiling C++ code), such as the"asm" and"typeof" keywords, and predefined macros such as"unix" and"vax" that identify the type of system you are using. It also enables the undesirable and rarely used ISO trigraph feature. For the C compiler, it disables recognition of C++ style // comments as well as the"inline" keyword.


要回答你的问题,没有unix不是C中的保留字。

但是,符号unix很可能是由预处理器定义的,因为您包含头文件或者因为编译器定义了它。


我对此非常谨慎,并猜测gcc在UNIX系统上有效#defined unix为1。

尝试

1
2
3
main(){
  printf("%d", unix);
}

看看你得到了什么。