How to find my current compiler's standard, like if it is C90, etc

我的梦境 提交于 2019-11-27 11:11:55

问题


I'm working on a Linux machine. Is there any system command to find the standard followed by the C compiler I'm using?


回答1:


This is compiler dependent, I'm supposing you're using GCC. You could check your compiler defined macros using:

gcc -dM -E - < /dev/null

Check the manual about the flags, specially:

__STDC_VERSION__

This macro expands to the C Standard's version number, a long integer constant of the form yyyymmL where yyyy and mm are the year and month of the Standard version. This signifies which version of the C Standard the compiler conforms to. Like STDC, this is not necessarily accurate for the entire implementation, unless GNU CPP is being used with GCC.

The value 199409L signifies the 1989 C standard as amended in 1994, which is the current default; the value 199901L signifies the 1999 revision of the C standard. Support for the 1999 revision is not yet complete.

This macro is not defined if the -traditional-cpp option is used, nor when compiling C++ or Objective-C.

In this site you can find a lot of information about this. See the table present here.




回答2:


You can also test this in your code using standard macros, for example (originally from sourceforge project of the same name):

#if defined(__STDC__)
# define PREDEF_STANDARD_C_1989
# if defined(__STDC_VERSION__)
#  define PREDEF_STANDARD_C_1990
#  if (__STDC_VERSION__ >= 199409L)
#   define PREDEF_STANDARD_C_1994
#  endif
#  if (__STDC_VERSION__ >= 199901L)
#   define PREDEF_STANDARD_C_1999
#  endif
#  if (__STDC_VERSION__ >= 201710L)
#   define PREDEF_STANDARD_C_2018
#  endif
# endif
#endif

If you want to check this from the command line you can pick one (e.g. c89) and check the return value from a minimal program:

echo -e "#ifdef __STDC__\n#error\n#endif"|gcc -xc -c - > /dev/null 2>&1; test $? -eq 0  || echo "c89



回答3:


At compile time, check against preprocessor macro:

  • __ANSI__
  • __STDC__
  • __STDC_VERSION__ >= 199901L for c99



回答4:


You probably have gcc, in which case you can specify the standard at compile-time, e.g.

$ gcc -Wall -std=c89 foo.c -o foo

or:

$ gcc -Wall -std=c99 foo.c -o foo

Type:

$ man gcc

for full details.




回答5:


If your C compiler is gcc, you can use the -std option to specify which C standard to follow. The default is gnu89. There's no general system command to determine the standard for any given compiler. You'll need to check the documentation.




回答6:


In addition to the above answers. If you would like to check if you are on a C99. Just define a struct and initialize it as:

struct Tree{
    struct Node *root;
    struct Node NIL_t;
    struct Node * const NIL;    
}

struct Tree t = { .root = NULL, .NIL = &t.NIL_t }; //init.

This works only on C99. If it doesnt, then you are not using a C99.



来源:https://stackoverflow.com/questions/4991707/how-to-find-my-current-compilers-standard-like-if-it-is-c90-etc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!