detecting standard support

C

copx

Is there are standard way to detect which C standard is supported by the
compiler?
I mean for conditional compilation like:

#if defined(C99)
# include "stdint.h"
#endif
 
G

Guest

copx said:
Is there are standard way to detect which C standard is supported by the
compiler?

You can use the __STDC_VERSION__ macro. For C99, it must be defined to
199901L.
I mean for conditional compilation like:

#if defined(C99)

#if __STDC_VERSION__ >= 199901L

Prior to C99, __STDC_VERSION__ was either undefined, or defined to
199409L. (Actually, it's possible that C89 implementations are allowed
to define __STDC_VERSION__ to whatever they want -- I'm not sure -- but
they would only do so if they are intentionally useless.)
 
C

copx

Harald said:
You can use the __STDC_VERSION__ macro. For C99, it must be defined to
199901L.


#if __STDC_VERSION__ >= 199901L
Thanks!

Prior to C99, __STDC_VERSION__ was either undefined, or defined to
199409L. (Actually, it's possible that C89 implementations are allowed
to define __STDC_VERSION__ to whatever they want -- I'm not sure -- but
they would only do so if they are intentionally useless.)


I think you mean <stdint.h>. It's not the same thing.

Yes, of course.
 
R

Robert Gamble

copx said:
Is there are standard way to detect which C standard is supported by the
compiler?
I mean for conditional compilation like:

#if defined(C99)
# include "stdint.h"
#endif

The 1989 Standard defines the __STDC__ predefined macro, this must be 1
on a conforming implementation. Amendment 1 to the 1989 Standard also
defines the predefined macro __STDC_VERSION__ which will be 199409L for
C89 Amendment 1 and 199901L for C99. You can use these two macros to
determine what version your implementation purportedly conforms to:

#include <stdio.h>

#if defined(__STDC__)
# if __STDC_VERSION__ >= 199901L
# define VERSION "C99 or greater"
# elif __STDC_VERSION__ >= 199409L
# define VERSION "C89 with Amendment 1"
# else
# define VERSION "C89"
# endif
#else
# define VERSION "Pre-C89"
#endif

int main (void) {
puts(VERSION);
return 0;
}

$ gcc -Wall -W -ansi -pedantic print_standard_version.c -o
print_standard_version
$ ./print_standard_version
C89

$ gcc -Wall -W -std=iso9899:199409 -pedantic print_standard_version.c
-o \
print_standard_version
$ ./print_standard_version
C89 with Amendment 1

$ gcc -Wall -W -std=c99 -pedantic print_standard_version.c -o
print_standard_version
$ ./print_standard_version
C99 or greater

Robert Gamble
 
C

copx

Robert said:
The 1989 Standard defines the __STDC__ predefined macro, this must be 1
on a conforming implementation. Amendment 1 to the 1989 Standard also
defines the predefined macro __STDC_VERSION__ which will be 199409L for
C89 Amendment 1 and 199901L for C99. You can use these two macros to
determine what version your implementation purportedly conforms to:
[snip]

Thanks!
 
K

Keith Thompson

Harald van Dijk said:
You can use the __STDC_VERSION__ macro. For C99, it must be defined to
199901L.


#if __STDC_VERSION__ >= 199901L

Prior to C99, __STDC_VERSION__ was either undefined, or defined to
199409L. (Actually, it's possible that C89 implementations are allowed
to define __STDC_VERSION__ to whatever they want -- I'm not sure -- but
they would only do so if they are intentionally useless.)

I was going to suggest that you should always test whether
__STDC_VERSION__ is defined before checking its value -- but then I
remembered that any undefined identifiers in a #if directive are
replaced with 0. So the test

#if __STDC_VERSION__ >= 199901L

will work properly whether __STDC_VERSION__ is defined or not.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top