14

The question title should say it all, but here's an example of what sort of thing I'm looking for:

#ifndef THE_IDENTIFIER_THAT_WOULD_INDICATE_BEING_COMPILED_AS_CPLUSPLUS

/*
 * Example of something that would matter.
 */
typedef enum _bool bool;
enum _bool { false, true };

#endif

What is the identifier? It's bugging me severely, as I know I've seen code that does this before.

I'm using GCC, by the way.

(I'm surprised I couldn't find a duplicate somewhere on SO. If someone else can find one, feel free to redirect me and close this as a dupe.)

4
  • 4
    It's __cplusplus. The macro is defined by C++ compilers. Duplicated from: stackoverflow.com/questions/3858308/… Commented Nov 10, 2010 at 21:27
  • 2
    btw: C99 added a native boolean type called _Bool, aliased to bool if you include <stdbool.h> Commented Nov 10, 2010 at 21:49
  • See also Preprocessor directive to test if this is C or C++ to learn about extern "C" and some ways to handle. Commented Sep 23, 2012 at 6:43
  • 1
    I find it amusing that 6 answers and a comment that states the answer all hit enter at the same time: Nov 10 '10 21:27 Commented Feb 1, 2019 at 21:30

6 Answers 6

18
#ifndef __cplusplus

If I remember correctly.

Sign up to request clarification or add additional context in comments.

2 Comments

it's typically used in the header file guards #ifdef __cplusplus\n extern "C" { \n#endif
+1 You remember correctly. Should be #ifndef for what William wants.
8

The identifier is __cplusplus

#ifdef __cplusplus
#error NO C++ PLEASE
#endif

C23 6.10.10.1p3

The implementation shall not predefine the macro __cplusplus, nor shall it define it in any standard header."

C++11 16.8.1

The following macro names shall be defined by the implementation:

__cplusplus : The name __cplusplus is defined to the value 201103L when compiling a C++ translation unit.

Comments

3

#ifdef __cplusplus

with a few really ancient compilers (early versions of cfront and a couple of ports) it was c_pluplus, IIRC.

Comments

2
#ifdef __cplusplus

Comments

2

#ifdef __cplusplus

I think the file extension matters too, if the C++ compiler is given a .c file it will compile it as C code. i have nothing to back this up though.

Comments

1

The identifier you are looking for is __cplusplus, which can be used like this:

#ifdef __cplusplus
// Code being compiled as C++.
#endif

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.