简体   繁体   中英

How can I know the version of c?

The "\b" didn't work on my Mac. So I tried to find the reason.

I think that cause of this problem may be the version of 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. To know which C version your program is running check the:

 __STDC_VERSION__

macro.

  • For C90: the macro is undefined.
  • For C99: the macro is defined with value 199901L.
  • For C11: the macro is defined with value 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).

Depending on your compiler it can support different C versions. 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 .

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

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

You open your terminal and paste this bash command:

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 ). Yours can be 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;
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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