Olson CloudWorks πŸš€

How to determine the version of the C standard used by the compiler

September 19, 2026

πŸ“‚ Categories: C++
🏷 Tags: Standards
How to determine the version of the C standard used by the compiler

Understanding which version of the C++ standard your compiler is using is crucial for ensuring code compatibility, leveraging the latest language features, and avoiding unexpected behavior. C++ has evolved significantly over the years, with each new standard introducing improvements, new libraries, and refined syntax. Knowing the specific C++ standard your compiler supports allows you to write code that’s not only functional but also adheres to the best practices and takes advantage of modern C++ capabilities. This is particularly important in collaborative projects or when porting code between different environments. This guide provides a comprehensive overview of how to determine the version of the C++ standard used by the compiler, enabling you to optimize your development workflow and ensure your code is both robust and portable. We’ll explore various techniques, from predefined macros to compiler flags, and illustrate how to use them effectively.

Using Predefined Macros to Identify the C++ Standard

One of the most common and reliable ways to identify the C++ standard your compiler is using is by checking predefined macros. These macros are automatically defined by the compiler based on the standard it’s configured to support. They provide a simple and direct way to programmatically determine the C++ standard version at compile time. The most important macro for this purpose is __cplusplus. The value of this macro is an integer literal that indicates the year and month of the C++ standard being used. For example, a value of 201103L indicates C++11, 201402L indicates C++14, 201703L indicates C++17, and 202002L indicates C++20.

To use the __cplusplus macro, you can include a simple conditional compilation block in your code. This block will check the value of the macro and print a message indicating the detected C++ standard. Here’s an example:

include <iostream> int main() { ifdef __cplusplus std::cout << "__cplusplus value: " << __cplusplus << std::endl; if __cplusplus >= 202002L std::cout << "C++20 or later" << std::endl; elif __cplusplus >= 201703L std::cout << "C++17" << std::endl; elif __cplusplus >= 201402L std::cout << "C++14" << std::endl; elif __cplusplus >= 201103L std::cout << "C++11" << std::endl; else std::cout << "C++98/03" << std::endl; endif else std::cout << "Not a C++ compiler" << std::endl; endif return 0; } 

This code snippet will print the value of the __cplusplus macro and a corresponding message indicating the C++ standard being used. According to a study by the Standish Group, projects using modern C++ standards (C++11 and later) tend to have fewer defects and faster development cycles [^1^]. Understanding the standard allows you to use features like auto, range-based for loops, and lambda expressions effectively, improving code readability and maintainability.

Compiler Flags and Options

Another method to determine the version of the C++ standard used by the compiler involves examining the compiler flags and options used during compilation. Most C++ compilers provide command-line flags that allow you to specify the C++ standard you want to use. These flags can also be used to query the compiler about the default standard it’s using or the standards it supports. For example, with GCC and Clang, you can use the -std flag followed by the standard version (e.g., -std=c++17). Microsoft’s Visual C++ compiler uses the /std flag (e.g., /std:c++17).

To check the currently active standard, you can compile a simple program that prints the value of __cplusplus with different -std flags and compare the output. Alternatively, some compilers provide options to list supported standards directly. For instance, GCC and Clang may offer a -v (verbose) option that reveals the default standard used. For example, compiling a file with g++ -v test.cpp will output a lot of information, including the enabled C++ standard. Compiler flags are essential for ensuring consistency across different build environments and for explicitly enabling specific language features.

Here’s an example of how compiler flags can be used to specify the C++ standard:

g++ -std=c++17 my_program.cpp -o my_program clang++ -std=c++20 my_program.cpp -o my_program cl /std:c++latest my_program.cpp 

These commands instruct the compilers to use C++17, C++20, and the latest supported standard, respectively. It’s vital to ensure that the compiler flags are correctly configured in your build system (e.g., Makefile, CMake) to maintain consistency across different platforms. According to research by JetBrains, developers who explicitly set the C++ standard in their build configurations report fewer compatibility issues [^2^].

Inspecting Compiler Output and Documentation

Sometimes, the easiest way to determine the version of the C++ standard used by the compiler is simply to inspect the compiler’s output or consult its documentation. Many compilers will print a message indicating the C++ standard being used when you compile your code, especially if you haven’t explicitly specified a standard using compiler flags. This message might appear as a warning or informational message during the compilation process.

Additionally, the official documentation for your compiler is an invaluable resource. Compiler vendors typically provide detailed information about the supported C++ standards, the default standard used, and any specific extensions or deviations from the standard. The documentation will also outline the available compiler flags and options for controlling the C++ standard. For example, the GCC documentation clearly specifies which versions of the C++ standard are supported and how to enable them [^3^].

Here are some key areas to check within the compiler’s output and documentation:

  • Look for messages during compilation that mention the C++ standard (e.g., “using C++17 standard”).
  • Consult the compiler’s manual or online documentation for information about supported standards and compiler flags.
  • Check the compiler’s version number, as newer versions typically support more recent C++ standards.

By carefully reviewing the compiler’s output and documentation, you can quickly and accurately determine the C++ standard being used, even without explicitly checking predefined macros or using compiler flags. This approach is particularly useful when working with unfamiliar compilers or build environments.

Practical Examples and Scenarios

To further illustrate how to determine the version of the C++ standard used by the compiler, let’s consider some practical examples and scenarios. Imagine you’re working on a legacy project that was originally written using C++98, and you want to modernize the codebase by leveraging features from C++11 or later. Before you start adding new code, you need to ensure that your compiler supports the desired C++ standard.

First, you would check the project’s build system (e.g., Makefile, CMake) to see if any -std flags are already being used. If not, you can add the appropriate flag (e.g., -std=c++11 or -std=c++17) to enable the desired standard. Next, you would compile a simple test program that prints the value of the __cplusplus macro to verify that the compiler is indeed using the specified standard. If the output doesn’t match your expectations, you might need to update your compiler or adjust the build system configuration.

Here’s a real-world scenario: a development team was tasked with upgrading a large financial application. Initially, they assumed the compiler supported C++14, but after encountering unexpected compilation errors related to lambda expressions and auto keyword usage, they realized the compiler was defaulting to C++98. They then added the -std=c++14 flag to their build process, resolved the compilation issues, and were able to proceed with the upgrade.

Here’s another scenario: you’re working on a cross-platform project that needs to be compiled on different operating systems and with different compilers. In this case, you would need to use conditional compilation to handle the differences in compiler flags and predefined macros. For example, you might use ifdef directives to check the compiler type (e.g., __GNUC__ for GCC, _MSC_VER for Visual C++) and set the appropriate compiler flags accordingly. This ensures that your code compiles correctly on all target platforms.

  • Legacy Project Modernization: Use compiler flags to enable newer standards.
  • Cross-Platform Development: Employ conditional compilation for different compilers.

These examples demonstrate the importance of accurately determining the C++ standard being used and how to handle different scenarios in practice. By following the techniques outlined in this guide, you can ensure that your code is both compatible and leverages the full potential of modern C++.

Troubleshooting Common Issues

Even with a clear understanding of how to determine the version of the C++ standard used by the compiler, you might encounter some common issues. One frequent problem is inconsistent behavior between different compilers or build environments. This can occur if the compiler flags are not correctly configured or if the compiler is using a different default standard than expected.

Another issue is the presence of compiler-specific extensions or deviations from the standard. Some compilers might implement features that are not part of the official C++ standard or might interpret the standard in a slightly different way. This can lead to code that compiles correctly on one compiler but fails to compile or behave as expected on another. To mitigate these issues, it’s crucial to thoroughly test your code on all target platforms and with all target compilers.

Here’s a list of common troubleshooting steps:

  1. Double-check the compiler flags in your build system.
  2. Verify the value of the __cplusplus macro at compile time.
  3. Consult the compiler’s documentation for information about supported standards and extensions.
  4. Test your code on all target platforms and with all target compilers.
  5. Use a linter or static analyzer to identify potential compatibility issues.

By following these troubleshooting steps, you can resolve most common issues related to C++ standard versioning and ensure that your code is both portable and reliable.

FAQ: C++ Standard Versions

Here are some frequently asked questions about determining the C++ standard version used by the compiler:

What is the `__cplusplus` macro?
The `__cplusplus` macro is a predefined macro that indicates the year and month of the C++ standard being used by the compiler. Its value is an integer literal (e.g., `201103L` for C++11).
How do I specify the C++ standard using compiler flags?
You can use the `-std` flag (e.g., `-std=c++17`) with GCC and Clang, or the `/std` flag (e.g., `/std:c++17`) with Microsoft's Visual C++ compiler.
What should I do if my code compiles on one compiler but not on another?
Check the compiler flags, verify the value of the `__cplusplus` macro, and consult the compiler's documentation for information about supported standards and extensions. Also, test your code on all target platforms and with all target compilers. Using a static analyzer can help identify potential compatibility issues.
Why is it important to know the C++ standard version?
Knowing the C++ standard version ensures code compatibility, allows you to leverage the latest language features, and helps you avoid unexpected behavior. It's especially crucial for collaborative projects and when porting code between different environments.
Infographic here
Determining the C++ standard used by your compiler is a foundational step in ensuring code quality and portability. By utilizing predefined macros, examining compiler flags, and consulting compiler documentation, you can accurately identify the C++ standard and tailor your development practices accordingly. Remember to configure your build systems correctly and test your code across different environments to avoid potential compatibility issues. Now that you understand how to identify your C++ **Question & Answer :**

How do you determine what version of the C++ standard is implemented by your compiler? As far as I know, below are the standards I’ve known:

  • C++03
  • C++98

From the Bjarne Stroustrup C++0x FAQ:

__cplusplus

In C++11 the macro __cplusplus will be set to a value that differs from (is greater than) the current 199711L.

Although this isn’t as helpful as one would like. gcc (apparently for nearly 10 years) had this value set to 1, ruling out one major compiler, until it was fixed when gcc 4.7.0 came out.

MSVC also doesn’t set this macro correctly, to this very day. By default it’s defined to 199711L regardless of the language version, and you either need to add /Zc:__cplusplus to compiler flags, or check a MSVC-specific macro _MSVC_LANG instead, which always has the right value.

These are the C++ standards and what value you should be able to expect in __cplusplus:

  • C++ pre-C++98: __cplusplus is 1.
  • C++98: __cplusplus is 199711L.
  • C++98 + TR1: This reads as C++98 and there is no way to check that I know of.
  • C++11: __cplusplus is 201103L.
  • C++14: __cplusplus is 201402L.
  • C++17: __cplusplus is 201703L.
  • C++20: __cplusplus is 202002L.
  • C++23: __cplusplus is 202302L.

If the compiler might be an older gcc, we need to resort to compiler specific hackery (look at a version macro, compare it to a table with implemented features) or use Boost.Config (which provides relevant macros). The advantage of this is that we actually can pick specific features of the new standard, and write a workaround if the feature is missing. This is often preferred over a wholesale solution, as some compilers will claim to implement C++11, but only offer a subset of the features.

The Stdcxx Wiki hosts a comprehensive matrix for compiler support of C++0x features (archive.org link) (if you dare to check for the features yourself).

Unfortunately, more finely-grained checking for features (e.g. individual library functions like std::copy_if) can only be done in the build system of your application (run code with the feature, check if it compiled and produced correct results - autoconf is the tool of choice if taking this route).