34

I am wondering how different the preprocessors for C++ and C are.

The reason for the question is this question on a preprocessor-specific question where the paragraph of the standard that addresses the question has a different wording (and a different paragraph number) and also are difference concerning the true and false keywords in C++.

So, are there more differences or is this the only difference.

An extension of the question would be when is a source file emitted differently by a C++ preprocessor and a C preprocessor.

4
  • That "extension" question is really the same as the main question! Commented Feb 23, 2011 at 0:02
  • Yes, but the question was originally driven by that "extension". Commented Feb 23, 2011 at 0:04
  • Although similar to true and false, there are also the named operators: and, and_eq, bitand, bitor, compl, not, not_eq, or, or_eq, xor and xor_eq. Commented Sep 13, 2011 at 7:38
  • Since C++11 not ending a translation unit w/ a newline is no longer undefined behavior Commented Feb 12, 2017 at 6:01

3 Answers 3

23

The C++03 preprocessor is (at least intended to be) similar to the C preprocessor before C99. Although the wording and paragraph numbers are slightly different, the only technical differences I'm aware of between the two are that the C++ preprocessor handles digraphs (two-letter alternative tokens) and universal character names, which are not present in C.

As of C99, the C preprocessor added some new capabilities (e.g., variadic macros) that do not exist in the current version of C++. I don't remember for sure, but don't believe that digraphs were added.

I believe C++0x will bring the two in line again (at least that's the intent). Again, the paragraph numbers and wording won't be identical, but I believe the intent is that they should work the same (other than retaining the differences mentioned above).

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

2 Comments

other than trigraphs, in C99 digraphs (but %: and the "quadrigraph" %:%:) are just tokens and not modified further in the preprocessing phase. They are then interpreted by the "real" compiler phases.
Also about C++03 vs C89, the C++ preprocessor has a special treatment of boolean literals.
7

They are supposed to be the same: C++98 and C++03 should match C90, and C++0x should match C99. There may be bugs in the wording, though.

Comments

7

Predefined macros differ between the preprocessors, mostly for obvious language feature differences. E.g. compare:

In particular:

  • C requires you not to define __cplusplus, C++ uses it to represent the version
  • C uses __STDC__ to represent the version, C++ says is implementation defined and uses __cplusplus instead
  • C has __STDC_IEC_559__ and __STDC_IEC_559_COMPLEX__ to indicate floating point characteristics, C++ does not and seems replace that with the per type std::numeric_limits<float>::is_iec559 constants
  • C does not have the macros prefixed with __STDCPP: _STDCPP_STRICT_POINTER_SAFETY__ and __STDCPP_THREADS__

As mentioned by DevSolar, C11 added many more defines which are not part of C++11.

4 Comments

Erm... you apparently forgot about C11... it's not only C++ that moved ahead. (Especially with regards to thread support and associated macros.)
@DevSolar thanks for the tip. Just to confirm: you mean that C11 added many more macros which are not in C++11 is that it? That's my impression on a quick look. Haven't looked into C++14, though, but there is stuff which won't be there for sure like __STDC_NO_VLA__
Well, both C11 and C++11 added threads, and the preprocessor macros necessary to handle them -- just different ones obviously. And I found it curious that you link C++11 documentation, but (outdated) C99 to compare. ;)
FYI: Syntax: C: # non-directive, C++: # conditionally-supported-directive.

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.