햄스터
햄스터

Reputation: 317

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.

Upvotes: 19

Views: 28372

Answers (7)

fun_times
fun_times

Reputation: 164

Based on what @chqrlie commented on Sunil Kumar Saini's answer, the macro __STDC_VERSION__ is not defined for C versions before C99, so we have to check this first to prevent compiler errors:

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

Upvotes: 5

Sunil Kumar Saini
Sunil Kumar Saini

Reputation: 71

int main()
{

    if (__STDC_VERSION__ >= 201710L)
        printf("We are using C17!\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;
}

Upvotes: 7

CUser
CUser

Reputation: 1

General method for checking the current C version, including extensions as C++ or augmentations as @C

#include <stdio.h>

int main(int argc, char *argv[], char *envp[]){

    //Needs revision and completion (with compiler name etc.)..

    #ifdef __augmented
        #define Prefix "@"//if is @C .. augmented version of C programming language etc.
    #else
        #define Prefix ""//not augmented
    #endif

    #ifdef __cplusplus
        #define Suffix "++"
        #if __cplusplus == 1
            #undef __cplusplus
            #define __cplusplus 199711
        #endif
        #define Number __cplusplus
        #define Year (__cplusplus / 100)
        #define Month (__cplusplus % 100)
        #define Type "CPP"
    #else
        #define Suffix ""
        #ifdef __STDC_VERSION__
            #define Number __STDC_VERSION__
            #define Year (__STDC_VERSION__ / 100)
            #define Month (__STDC_VERSION__ % 100)
            #define Type "STD"
        #else
            #define Number 0
            #define Month 0
            #ifdef __STRICT_ANSI__
                #define Year 1989
                #define Type "ANSI"
            #else
                #ifdef __STDC__
                    #define Year 1990
                    #define Type "ISO"
                #else
                    #define Year 1972
                    #define Type "K&R"
                #endif
            #endif
        #endif
    #endif

    printf("%sC%s%02ld %ld/%02ld %s %ld\n", Prefix, Suffix, Year % 100, Year, Month, Type, Number);

    return 0;
}

Upvotes: 0

Federico Ba&#249;
Federico Ba&#249;

Reputation: 7636

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

Upvotes: 1

Wadia
Wadia

Reputation: 1

#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;
  }

Upvotes: -1

winterr_dog
winterr_dog

Reputation: 79

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

Upvotes: 2

ouah
ouah

Reputation: 145829

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.

Upvotes: 30

Related Questions