Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print c with gcc and c++ with g++

Tags:

c++

c

gcc

g++

Can anybody tell how to write a program which when compiled with gcc prints c , and with g++ prints c++?

like image 402
akash Avatar asked Dec 07 '25 08:12

akash


2 Answers

#ifdef __cplusplus
    printf("c++\n");
#else
    printf("c\n");
#endif

You may run into issues if your file extension isn't right.

like image 61
James Avatar answered Dec 08 '25 20:12

James


Something like this:

#if __cplusplus
    printf("c++");
#else 
    printf("c");
#endif

Unless you're compiling with g++ -x c which it will still print C even though compiled with g++. That's a gotcha.

like image 27
Tony The Lion Avatar answered Dec 08 '25 22:12

Tony The Lion



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!