用gcc和c ++打印c和g ++

aka*_*ash 1 c c++ gcc g++

任何人都可以告诉如何编写一个程序,当编译gcc打印cg++打印c++

Jam*_*mes 8

#ifdef __cplusplus
    printf("c++\n");
#else
    printf("c\n");
#endif
Run Code Online (Sandbox Code Playgroud)

如果您的文件扩展名不正确,您可能会遇到问题.


Ton*_*ion 6

像这样的东西:

#if __cplusplus
    printf("c++");
#else 
    printf("c");
#endif
Run Code Online (Sandbox Code Playgroud)

除非你正在编译g++ -x c它仍然会打印C,即使使用g ++编译.那是一个问题.


Jen*_*edt 5

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)