c++ccompiler-specific

Write a program that will print "C" if compiled as an (ANSI) C program, and "C++" if compiled as a C++ program


Taken from http://www.ocf.berkeley.edu/~wwu/riddles/cs.shtml

It looks very compiler specific to me. Don't know where to look for?


Solution

  • Simple enough.

    #include <stdio.h>
    int main(int argc, char ** argv) {
    #ifdef __cplusplus
    printf("C++\n");
    #else
    printf("C\n");
    #endif
    return 0;
    }
    

    Or is there a requirement to do this without the official standard?