Hello today I will show you how to write a C program to get the C version you are using. This is a useful program that can help you check the standard compliance of your C compiler and the features it supports. here, I will explain the code of the program, how to compile and run it, and what output to expect.
First, let's look at the code of the program. We start by including the stdio.h header file, which provides us with the printf function to print data on the screen. Then we define the main function, which is the entry point of our program.
Inside the main function, we use a conditional compilation directive called #ifdef to check if a predefined macro called __STDC_VERSION__ is defined or not. A macro is a symbolic name that represents some value or expression. A predefined macro is a macro that is defined by the compiler or the standard library. The __STDC_VERSION__ macro is a predefined macro that indicates the version of the C standard that the compiler conforms to. The value of this macro is a long integer constant that encodes the date of the standard. For example, 201112L means C11 (ISO/IEC 9899:2011), 199901L means C99 (ISO/IEC 9899:1999), and so on. If this macro is not defined, it means the compiler does not conform to any standard.
If the __STDC_VERSION__ macro is defined, we use the printf function to print "The C version you are using is: C%ld\n", where %ld is a format specifier for a long integer, and \n is a newline character. We also use the / operator to divide the value of __STDC_VERSION__ by 100L, which is a long integer literal for 100. This gives us the year of the standard without the century part. For example, 201112L / 100L gives us 2011.
If the __STDC_VERSION__ macro is not defined, we use the printf function to print "The compiler does not conform to any standard\n". Finally, we return 0 from the main function to indicate successful termination of the program.
That's all for the code of the program. Now let's see how to compile and run it. To compile and run the program, we need a C compiler and a terminal. I have saved the program in a file called c_version.c, so I will use this command on a terminal:
gcc c_version.c -o c_version
This will compile the program and create an executable file called c_version. To run this file, I will use this command:
./c_version
This will execute the program and print the output on the screen. Depending on your compiler and system, you may get different outputs. For example, on my system, I get this output:
The C version you are using is: C11
This means that my compiler conforms to the C11 standard.
C Program
#include <stdio.h>
int main()
{
// Use the predefined macro __STDC_VERSION__ to get the C version
// The value of this macro is a long integer constant that encodes the date of the standard
// For example, 201112L means C11 (ISO/IEC 9899:2011), 199901L means C99 (ISO/IEC 9899:1999), etc.
// If this macro is not defined, it means the compiler does not conform to any standard
#ifdef __STDC_VERSION__
printf("The C version you are using is: C%ld\n", __STDC_VERSION__ / 100L);
#else
printf("The compiler does not conform to any standard\n");
#endif
return 0;
}
Output
The C version you are using is: C11
Flowchart
- Start
- Include stdio.h
- Define main function
- Check if __STDC_VERSION is defined
- Print "The C version you are using is: C%ld\n" with __STDC_VERSION__100L as the argument
- Return 0
- End
And that's it! We have successfully written and run a C program to get the C version we are using. I hope you enjoyed it and learned something new.
We love your feedback and invite you to comment on our articles, exercises, examples, quizzes and others. Your feedback helps us make our content awesome and serve you better. Please leave a comment and tell us what you think. How did our content help you learn something new? Thank you for being a part of our community!