A recent compilation error encountered while attempting to compile a C code has raised the question of what the default C standard is when using the g compiler. The code involved includes file stream and string manipulation. When compiled using the command 'g example.cpp', the compilation failed due to errors involving conversion between strings and const char*. However, using the command 'g -std=c 17 example.cpp' resolved the issue.
To investigate the default standard used by g when the -std flag is not specified, a method is presented to determine the default version of the C standard supported by a particular version of g :
Bash: Execute the following command:
g++ -dM -E -x c++ /dev/null | grep -F __cplusplus
PowerShell: Run the following command:
g++ -dM -E -x c++ NUL | Select-String __cplusplus
The output of these commands will display a line similar to:
#define __cplusplus 199711L
The value following '__cplusplus' indicates the default C standard version. In this example, it is C 98 (199711).
As a programmer, the decision of whether or always specify the -std flag depends on several factors:
In general, it is recommended to specify the C standard explicitly using the -std flag to avoid unexpected behavior due to implicit default settings. By controlling the C standard, programmers can ensure optimal performance, code correctness, and portability.
The above is the detailed content of What C Standard Does g Default To?. For more information, please follow other related articles on the PHP Chinese website!