How can you tell whether a program was compiled using C versus C++?


The ANSI standard for the C language defines a symbol named __cplusplus that is defined only when you are compiling a C++ program. If you are compiling a C program, the __cplusplus symbol is undefined. Therefore, you can check to see whether the C++ compiler has been invoked with the following method:

#ifdef __cplusplus                      /* Is __cplusplus defined? */
#define USING_C FALSE                   /* Yes, we are not using C */
#define USING_C TRUE                    /* No, we are using C */

When the preprocessor is invoked, it sets USING_C to FALSE if the __cplusplus symbol is defined. Otherwise, if __cplusplus is undefined, it sets USING_C to TRUE. Later in your program, you can check the value of the USING_C constant to determine whether the C++ compiler is being used.


© 2017