is “unix” restricted keyword in C?

2019-01-23 11:25发布

This code does not compile for me on gcc version 4.3.2 (Debian 4.3.2-1.1)

main(){
  int unix;
}

I've checked the C keywords list and "unix" is not one of them. Why am I getting the following error?

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

Anybody?

标签: c unix gcc keyword
6条回答
太酷不给撩
2楼-- · 2019-01-23 12:07

unix is one of the defines the preprocessor uses in gcc to get a list of defs use

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

(-dM tells gcc to debugdump the defs -E tells it to stop after prepreocessing and -x c /dev/null tells him to pretend /dev/null is a c file)

查看更多
姐就是有狂的资本
3楼-- · 2019-01-23 12:13

To answer your question, no unix is not a reserved word in C.

However, the symbol unix is most likely defined by the preprocessor either because you include a header file or because the compiler defines it.

查看更多
劳资没心,怎么记你
4楼-- · 2019-01-23 12:19

Run your code through the preprocessor to find out what the compiler is actually seeing:

gcc -E unix.c

Then see if your variable unix is preserved or converted by the preprocessor.

查看更多
闹够了就滚
5楼-- · 2019-01-23 12:21

I'm gona take a wild stab at this and guess that gcc effectively #defined unix as 1 on UNIX systems.

try

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

and see what you get.

查看更多
甜甜的少女心
6楼-- · 2019-01-23 12:22

unix is not a identifier reserved by the Standard.

If you compile with -std=c89 or -std=c99 the gcc compiler will accept the program as you expected.

From gcc manual ( https://gcc.gnu.org/onlinedocs/cpp/System-specific-Predefined-Macros.html ), the emphasis is mine.

... 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__.

查看更多
何必那么认真
7楼-- · 2019-01-23 12:25

It is not a keyword.

It is a predefined macro to identify the type of system. On Unix and Unix like systems it is defined to be 1.

To disable this use the -ansi option:

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.

查看更多
登录 后发表回答