#ifdef __cplusplus
printf("c++\n");
#else
printf("c\n");
#endif
Run Code Online (Sandbox Code Playgroud)
如果您的文件扩展名不正确,您可能会遇到问题.
像这样的东西:
#if __cplusplus
printf("c++");
#else
printf("c");
#endif
Run Code Online (Sandbox Code Playgroud)
除非你正在编译g++ -x c它仍然会打印C,即使使用g ++编译.那是一个问题.
struct标签的处理在C和C++之间不同
#include<stdio.h>
typedef int T;
int main(void) {
struct T { int a[2]; };
puts((sizeof(T) > sizeof(int)) ? "C++" : "C");
return 0;
}
Run Code Online (Sandbox Code Playgroud)