The C standard Standard defines a set of predefined macros (see subclause 6.10.8) to help the user determine if the implementation being used is a Standard conforming implementation, and if it is so, to which version of the C Standard it conforms. These macros can all so also help the user to determine which of the standard features are implemented.
The tables below list these macros and which version of the C Standard they were introduced. The following macros are required in C11.
| C89 | C99 | C11 |
|
| C99 | C11 |
| (C94) | C99 | C11 |
| C89 | C99 | C11 |
| C89 | C99 | C11 |
| C89 | C99 | C11 |
| C89 | C99 | C11 |
The following are optional environment macros in C11.
|
| C99 | C11 |
|
| C99 | C11 |
|
|
| C11 |
|
|
| C11 |
The following are optional feature macros in C11.
|
|
| C11 |
|
| C99 | C11 |
|
| C99 | C11 |
|
|
| C11 |
|
|
| C11 |
|
|
| C11 |
|
|
| C11 |
|
|
| C11 |
The following is optional in C11 and is defined by the user:
|
|
| C11 |
Noncompliant Code Example (Checking value of predefined macro)
The value a C Standard predefined macro should never be tested for a value before the macro is tested to make sure it is defined, as shown in this noncompliant code example:
...
In this compliant example, the definition of the predefined macro __STDC__
is tested before the value of the macro is tested:
Code Block | ||||
---|---|---|---|---|
| ||||
#include <stdio.h> int main(void) { #if defined(__STDC__) #if (__STDC__ == 1) printf("Implementation is ISO-conforming.\n"); #else printf("Implementation is not ISO-conforming.\n"); #endif #else /* !defined(__STDC__) */ printf("__STDC__ is not defined.\n"); #endif /* ... */ return 0; } |
...
The follow example tests to see if the C11 predefined macro __STDC_ANALYZABLE__
defined and what value the implementation has given the macro:
Code Block | ||||
---|---|---|---|---|
| ||||
#include <stdio.h> int main(void) { #if defined (__STDC__) #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) /* C11 */ #if defined(__STDC_ANALYZABLE__) #if (__STDC_ANALYZABLE__ == 1) printf("Compiler conforms to Annex L (Analyzability).\n"); #else printf("Compiler does not support Annex L (Analyzability).\n"); #endif #else printf("__STDC_ANALYZABLE__ is not defined.\n"); #endif #else printf("Compiler not C11.\n"); #endif #else printf("Compiler not Standard C.\n"); #endif return 0; } |
...
Code Block | ||||
---|---|---|---|---|
| ||||
#if defined(__STDC_LIB_EXT1__)
#if (__STDC_LIB_EXT1__ >= 201112L)
#define USE_EXT1 1
#define __STDC_WANT_LIB_EXT1__ 1 /* want the ext1 functions */
#endif
#endif
#include <string.h>
#include <stdlib.h>
int main(void) {
char source_msg[] = "This is a test.";
char *msg = malloc(sizeof(source_msg) + 1);
if (msg != NULL) {
#if defined(USE_EXT1)
strcpy_s(msg, sizeof msg, source_msg);
#else
strcpy(msg, source_msg);
#endif
}
else {
return EXIT_FAILURE;
}
return 0;
}
|
...
Related Guidelines
ISO/IEC TR 24772:2013 | Pre-processor Directives [NMP] |
[ISO/IEC 9899:2011] | 6.10.8, "Predefined macro names" K.3.7.1, "Copying functions" |
...