简体   繁体   English

我怎么知道c的版本?

[英]How can I know the version of c?

The "\b" didn't work on my Mac. "\b"在我的 Mac 上不起作用。 So I tried to find the reason.所以我试图找到原因。

I think that cause of this problem may be the version of C.我认为导致这个问题的原因可能是C的版本。

Or device could be.或者设备可能是。 If you know it, can you help me?如果你知道,你能帮帮我吗? Thank you.谢谢你。

There are three ISO standard versions of C: C90, C99 and C11. C有三个ISO标准版本:C90,C99和C11。 To know which C version your program is running check the: 要知道您的程序正在运行哪个C版本,请检查:

 __STDC_VERSION__

macro. 宏。

  • For C90: the macro is undefined. 对于C90:宏未定义。
  • For C99: the macro is defined with value 199901L. 对于C99:宏定义为值199901L。
  • For C11: the macro is defined with value 201112L. 对于C11:宏定义为值201112L。

On the other hand if what you want to know is the version not of C but the version of your C compiler, as the other answers suggests, run the compiler with the appropriate option ( --version for both gcc and clang for example). 另一方面,如果您想知道的不是C的版本,而是C编译器的版本,如其他答案所示,请使用适当的选项运行编译器(例如gccclang都使用--version )。

Depending on your compiler it can support different C versions. 根据您的编译器,它可以支持不同的C版本。 You can ask to change the compiler default C version used for compiling using the -std= option with gcc and clang , for example: -std=c90 , -std=c99 or -std=c11 . 您可以要求使用带有gccclang-std= 选项来更改用于编译的编译器默认C版本,例如: -std=c90-std=c99-std=c11

int main() { if (__STDC_VERSION__ >= 201710L) printf("We are using C18!\n"); else if (__STDC_VERSION__ >= 201112L) printf("We are using C11!\n"); else if (__STDC_VERSION__ >= 199901L) printf("We are using C99!\n"); else printf("We are using C89/C90!\n"); return 0; }

based on what @chqrlie added...基于@chqrlie 添加的内容...

#include <stdio.h>

int main() {
  #if defined __STDC_VERSION__ 
    long version = __STDC_VERSION__;
    if ( version == 199901 ) {
      printf ("version detected : C99\n");
    }
    if ( version == 201112 ) {
      printf ("version detected : C11\n");
    }
    if ( version == 201710 ) {
      printf ("version detected : C18\n");
    }
  #else 
    printf ("version detected : C90\n");
  #endif
}

Prerequisites : gcc should be installed.先决条件:应安装gcc

You open your terminal and paste this bash command:您打开终端并粘贴此 bash 命令:

gcc -dM -E - < /dev/null | grep __STDC_VERSION__ | awk '{ print $2 " --> " $3 }'

For my case it returns __STDC_VERSION__ --> 201710L which translates to the 2017 C standard( c17 ).对于我的情况,它返回__STDC_VERSION__ --> 201710L转换为2017 C 标准 ( c17 )。 Yours can be c89 or c99 or c11你的可以是c89 or c99 or c11

Slighly cleaner and bit more complete approach稍微更清洁和更完整的方法

#include <stdio.h>

int main(void) {

    #ifdef __STDC_VERSION__
        switch (__STDC_VERSION__) {
            case 199409:
                printf ("C version: C94 (%ld)", __STDC_VERSION__);
                break;
            case 199901:
                printf ("C version: C99 (%ld)", __STDC_VERSION__);
                break;
            case 201112:
                printf ("C version: C11 (%ld)", __STDC_VERSION__);
                break;
            case 201710:
                printf ("C version: C17 (%ld)", __STDC_VERSION__);
                break;
            default:
                printf ("C version: ?? (%ld)", __STDC_VERSION__);
                break;
        }
    #else
        printf ("C(89), C(90)");
    #endif

    #ifdef __STRICT_ANSI__
        printf (" (ANSI %d)\n", __STDC__);
    #else
        printf("\n");
    #endif


    return 0;
}

Test it with:测试它:

gcc main.c -o main -std=c90 && main
gcc main.c -o main -std=c99 && main
gcc main.c -o main -std=c11 && main
gcc main.c -o main -std=c17 && main

gcc main.c -o main -std=gnu89 && main
gcc main.c -o main -std=gnu99 && main
gcc main.c -o main -std=gnu11 && main
gcc main.c -o main -std=gnu17 && main
#include <stdio.h> 
//What version of C language you're using
int main() {
  if(__STDC_VERSION__ >=201710L)
    printf("The version is c18!\n");
  else if(__STDC_VERSION__ >= 201112L)
        printf("The version is C11! \n");
        else if (__STDC_VERSION__ >=199901L)
             printf("The version is C99!\n");
              else 
             printf("The version you're using is C89/C90");
 return 0;
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM