144

I understand C and C++ are different languages but when I was learning C++ I was always told that C is a subset of C++ or C++ is C with classes. And that was quite true until the appearance of C++x0, C++11 (or the modern C++ 11/14/17 in general). In fact (especially when working on embedded systems) it's very likely to find code written in C++ but with a lot of parts written entirely in pure C language. Here I have several questions:

  1. Should I stop using the term C/C++?
  2. If the answer to #1 is yes, how would I call a program that use a mix of C and C++?
  3. Given that both of them are 'different' languages is it likely that at some point C++ compilers stop supporting code written in the C language (since modern c++ is diverging from the C mentality for basic stuff like pointers, dynamic memory handling, etc)
  4. Is there right now any collaboration between the people who makes the standards of C/C++ to keep the compatibility
  5. If #4 is yes, such collaboration could end up in the near future with the appearance of the modern c++ (11/14/17)

I know that there already similar questions, but I'm sure that a lot of people share these questions so I'm very interested to get good answers especially for the points that have to do with the C++ tendency in the near future.

gnat
  • 21,442
  • 29
  • 112
  • 288
rkachach
  • 1,219
  • 2
  • 9
  • 9
  • 36
    *And that was quiet true until the appearance of C++x0, C++11* No, C89 is not a subset of C++98. –  Sep 30 '15 at 19:29
  • 1
    Migrated here as questions such as [How is C different from C++?](http://programmers.stackexchange.com/questions/38942/how-is-c-different-from-c) appear to be on-topic – user229044 Sep 30 '15 at 21:20
  • 2
    @ouah Even modulo minor differences resulting in easily-correctable compilation errors? – user253751 Oct 01 '15 at 02:38
  • 36
    `Should I stop using the term C/C++`. Yes. This is only used by recruiters and HR. Engineers will use the term `C` or `C++` as independent languages. If you find an engineer that mixes the term avoid them. – Martin York Oct 01 '15 at 06:43
  • 2
    Yes, you should stop using the term "C/C++" unless you are referring specifically to [the typeless language by that name](http://archive.is/1uyXe) which has no relation to either C or C++. – greyfade Oct 01 '15 at 06:47
  • 8
    @LokiAstari: really? I guess SQLite developers are HR, since they do not have open positions. This is an incredibly restrictive view (please go and downvote my answer as others in your situation did). –  Oct 01 '15 at 07:40
  • I'm asking about the term because it's widely used (specially in the last two decades) .. from the answers you can see that there're a lot of people who agree on using it (but knowing that both of them are different languages). – rkachach Oct 01 '15 at 09:10
  • See my answer below, there are plenty of perfectly good reasons why you might quite reasonably want to use the term C/C++ – Ben Oct 01 '15 at 10:29
  • 2
    Ad 2. How woyld you call program that uses mix of, say, Python and JavaScript? (virtually any web application today) – el.pescado - нет войне Oct 01 '15 at 13:39
  • 4
    @greyfade: It's bad to assume that anyone, anywhere can lay claim to a commonly seen sequence of characters. "C/C++" may be the name chosen for a certain language, but that doesn't mean that usage of "C/C++" refers to that language, any more than if I copied that website and replaced "C/C++" by "C++1z" that all discussion of the draft C++ standard would suddenly be using wrong terminology. Simply put, the string "C/C++" never has referred to that joke language named "C/C++" and never will. – Ben Voigt Oct 01 '15 at 15:52
  • 3
    I'm surprised by how many people say they learned at some point that "C was a subset of C++." I distinctly remember learning back in high school Comp Sci that "C++ was a superset added to C." C came first didn't it? You can't really derive something from something else that doesn't exist yet. –  Oct 01 '15 at 16:40
  • 2
    If you're talking about things in common between C and C++, then not calling it C/C++ is silly. – user253751 Oct 01 '15 at 18:30
  • 1
    not only should you stop using the term, you should never have started using it unless in the context of a mixed project where both languages are used side by side. – jwenting Oct 01 '15 at 18:43
  • 33
    @TomDworzanski, http://www.stroustrup.com/bs_faq.html#C-is-subset "In the strict mathematical sense, C isn't a subset of C++...However, C++ supports every programming technique supported by C... It is not uncommon to be able to convert tens of thousands of lines of C to C-style C++ in a few hours. Thus, C++ is as much a superset of ANSI C as ANSI C is a superset of K&R C and much as ISO C++ is a superset of C++ as it existed in 1985. Well written C tends to be legal C++ also. For example, every example in Kernighan & Ritchie: "The C Programming Language (2nd Edition)" is also a C++ program." – Ben Oct 01 '15 at 19:21
  • 1
    A project that has significant amounts of both C and C++ source code is **written in both C and C++** (one that is primarily C++ with a little bit of C is simply **written in C++**). A single source file that is both valid C and valid C++—and provides the same functionality and behavior in both languages—is **a [polyglot](https://codegolf.stackexchange.com/questions/tagged/polyglot)**. – Blacklight Shining Oct 01 '15 at 19:54
  • 1
    @BenVoigt: You're missing the point: That language was created *because* the string "C/C++" was so badly misused, so as to point out to people the egregiousness of their error. – greyfade Oct 02 '15 at 01:35
  • 4
    @grayfade: There is no such language, there's an rant about "C/C++" hidden in the guise of a language specification, but it's not meant to be a language. And it does not redefine "C/C++" for the rest of the world. Now, if you want to say "recommended reading on C/C++ -> link to essay" to anyone using the term, that's more reasonable than claiming they are talking about a joke language when they obviously mean some unclear combination of the two real languages C and C++. And even that is questionable, when the essay has been taken down and you need to use an archive to find it. – Ben Voigt Oct 02 '15 at 01:38
  • 1
    i gotta agree with @immibis and polyglot. the opposite advice is coming from wankers. – robert bristow-johnson Oct 02 '15 at 04:38
  • 2
    Maybe just stop using the term C/C++ around pedantic jerks? – user1172763 Oct 02 '15 at 13:38
  • 2
    I'd argue that when writing C, making sure what you write is _also_ valid C++ is a good practice. It increases verbosity, but also makes the code more explicit in many cases (e.g. the internal vs. external linkage defaults). This is especially true for headers, which, when carefully written, can be used as an API for both languages with help of just a few `#ifdef __cplusplus \n extern "C" { \n #endif` snippets. Source files are not as critical, but it may help when migrating or integrating projects. – kFYatek Oct 02 '15 at 15:44
  • 1
    In my resume, in the list of skills, I put "C/C++" - where talking about individual projects, I specify the specific language, or where the project used both, "C and C++". This allows my resume to be found by keyword searchers, but now I worry that managers may be throwing it out because they disagree with use of this term. – Dewi Morgan Oct 03 '15 at 06:15
  • The slash used in the English language is not an operator with a single specific meaning, nor is it overloaded. It's shorthand for "or" or "and". – barbecue Oct 03 '15 at 19:46
  • The one context in which I use "C/C++", and will continue to use it, is when contrasting the two of them against, for example, "Java / C#". The former are close-to-the-metal, system programming languages. The latter aren't. – DevSolar Oct 05 '15 at 13:00
  • 1
    Sometimes people rewrite their C in order to be C++-compiler compatible by adding casts for malloc and so on. It's possible *that's* what some people mean when they mean by "C/C++". I, however, prefer to call that file C++'d C. – Brandin Oct 05 '15 at 13:31
  • @kFYatek I would argue that C code that can compile as C++ is badly written C code (e.g. casting the return value of malloc is a bad practice). The two languages have so different philosophies that combining them creates a mess. – martinkunev Oct 05 '15 at 21:30
  • 1
    I was always taught that C++ was an extension of C... which it was. Now it is a completely different beast. – Luke Oct 06 '15 at 15:26

11 Answers11

189

C was never a subset of C++. The most obvious example of this is int new;. This has been true since C89 and C++98, and the languages have only grown further from each other as new standards have come out.

Should I stop using the term C/C++

Yes

If the answer to #1 is yes, how would I call a program that use a mix of C and C++?

A source file is written in one language or the other. A program can consist of code from multiple languages working together, or an executable produced by linking different compiled objects. You would say the program was written in C and C++, "C/C++" is not a language.

Given that both of them are 'different' languages is it likely that at some point C++ compilers stop supporting code written in the C language

  1. They never did. char *a = malloc(10);. C and C++ have never been fully compatible for at least as long as they've had ISO standards (I don't know all the details about the pre-standardized days). click the links or see below for a file that is fine with C89 and up, but isn't valid under any C++ standard.

  2. afaik no, but I don't know much about the C working group.

/* A bunch of code that compiles and runs under C89 but fails under any C++ */

/* type aliases and struct names occupy separate namespaces in C, not in C++ */
struct S { int i; };
typedef int S;


struct Outer { struct Inner { int i; } in; };
/* struct Inner will be Outer::Inner in C++ due to name scope */
struct Inner inner;


/* default return type of int in C, C++ functions need explicit return types */
g() {
    return 0;
}


/* C sees this as two declarations of the same integer,
 * C++ sees it as redefinition */
int n;
int n;


/* K&R style argument type declarations */
void h(i) int i; { }


/* struct type declaration in return type */
struct S2{int a;} j(void) { struct S2 s = {1}; return s; }


/* struct type declaration in argument, stupid and useless, but valid */
/*void dumb(struct S3{int a;} s) { } */


/* enum/int assignment */
enum E{A, B};
enum E e = 1;


void k() {
    goto label; /* C allows jumping past an initialization */
    {
        int x = 0;
label:
        x = 1;
    }
}


/* () in declaration means unspecified number of arguments in C, the definition
 * can take any number of arguments,
 * but means the same as (void) in C++  (definition below main) */
void f();

int main(void) {
    f(1); /* doesn't match declaration in C++ */
    {
        /* new is a keyword in C++ */
        int new = 0;
    }

    /* no stdio.h include results in implicit definiton in C.  However,
     * as long as a matching function is found at link-time, it's fine.
     * C++ requires a declaration for all called functions */
    puts("C is not C++");
    {
        int *ip;
        void *vp = 0;
        ip = vp; /* cast required in C++, not in C */
    }
    return 0;
}

/* matches declaration in C, not in C++ */
void f(int i) { }

I always feel it's worth mentioning that C is a subset of Objective-C.

xhainingx
  • 1,432
  • 1
  • 9
  • 9
  • 62
    Objective-C was *specifically* designed to be a strict superset of C, with no conflicting syntax and a fully orthogonal object system. This is taken to such an extreme that you can actually take the "Objective" part of "Objective-C" and bolt it on to other languages, creating e.g. Objective-MODULA-2. And most famously, Apple's Objective-C++ which features *two* completely orthogonal, non-interacting, non-integrated object systems. – Jörg W Mittag Sep 30 '15 at 21:30
  • 21
    @masonwheeler if OP wants to see what an actual superset of C looks like. – xhainingx Sep 30 '15 at 22:05
  • 1
    Nice, thorough, answer - although I think just this gif would have sufficed: [No! God no!](http://i.giphy.com/12XMGIWtrHBl5e.gif) – Ben Collins Sep 30 '15 at 23:41
  • 27
    "A source file is written in one language or the other." Tell that to my program which cleanly compiles as both standard C99 and C++89. Neither language is a subset of the other, but there is an *intersection* of their two sets that is widely targeted. See Lua, et al. – munificent Oct 01 '15 at 00:42
  • 29
    @munificent I can write C++ code which can run through `javac` too, but what would be the point? – xhainingx Oct 01 '15 at 00:48
  • 23
    @munificent An example of a program that can be compiled in three languages - C, /bin/sh, and f77 - is the applin.c entry to the IOCCC 1986. According to your definition, the tag `C/Fortran/sh` makes sense now?! http://www.ioccc.org/years-spoiler.html#1986 . – Sjoerd Oct 01 '15 at 01:03
  • 1
    Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackexchange.com/rooms/29800/discussion-on-answer-by-xhainingx-should-i-stop-using-the-term-c-c). – yannis Oct 02 '15 at 10:03
  • @Jörg: objC++ 14 2.0 - maybe the most beautiful language ever when it comes to writing obfuscated code :-D – Kaiserludi Oct 02 '15 at 13:40
  • 2
    Guys, a program that can be compiled in multiple languages is called a [Polyglot](https://en.wikipedia.org/wiki/Polyglot_%28computing%29). There exists a polyglot that compiles in **16 different languages**. – Vorac Oct 02 '15 at 13:48
  • 1
    Please fix inconsistency: "_They never did. _... _C and C++ have long since diverged._" Could C++ compilers never compile C code or could they for a while but cannot anymore? Or is it a question of symantics, C++ could not compile C code files, but could compile most C code blocks/samples? – Trisped Oct 02 '15 at 18:01
  • 4
    This contains a technical inaccuracy. C++ is the successor to C with Classes which was little more than a macro system for C. You can read Bjarne Stroustrop's version of events in http://www.stroustrup.com/hopl2.pdf specifcally "Having C as a subset dramatically reduced the support and documentation work needed." and "The first demand to emerge from development management was that of 100% compatibility with C." and "an agreement emerged that there would be no gratuitous incompatibilities between C++ and ANSI C." For a time, C was a strict subset of C++. – corsiKa Oct 02 '15 at 20:43
  • 7
    AFAIK, most people use the term "C/C++" as a shorter way to say "C and C++". It's just shorter to write. Last time I checked, programmers still liked short/abbreviated names --see what I did there? ;) – code_dredd Oct 02 '15 at 20:50
  • 2
    @ray: Exactly (+1). And since C is very similar to a subset of C++, one normally expects that if you know C++, you are also proficient in C. – Giorgio Oct 02 '15 at 21:49
  • 3
    *"A source file is written in one language or the other."* ← It can also be written in a subset of both, as is very often the case. After having had to deal with a C library that had painfully C++-incompatible headers, I think this is an important point. – Szabolcs Oct 04 '15 at 14:56
  • 2
    @Giorgio Really? Do people really expect that if you know one, you know the other? Anyone who has programmed in both knows this is absolutely not the case. You might be much better equipped to pick the other up, but I would not say "You know C++, ergo you know C" - that's absolutely silly talk. That hasn't been the case for nearly 30 years. – corsiKa Oct 05 '15 at 16:40
  • 3
    @Giorgio that's a dangerous assumption, but I've met a lot of programmers that know C++ and assume they know C, but couldn't `char*` their way out of a paper bag – xhainingx Oct 05 '15 at 17:24
  • @corsiKa: Maybe you are right: being familiar with the C-like subset of C++ would make the switch to C very easy, but since this subset is not used very often, companies write C / C++ explicitly if they want to be sure that a programmer knows both languages. – Giorgio Oct 05 '15 at 20:14
  • See the accepted answer of http://stackoverflow.com/questions/2406711/make-my-file-readable-as-either-perl-or-html for an example of how to write a file that is valid and useful as either Perl or HTML – JoelFan May 04 '16 at 00:00
  • @xhainingx If you can do it *without* just writing the program twice in a way that appears as a comment in one language, that is a valid thing to do and your program is now C++/Java. – user253751 Nov 05 '20 at 09:42
  • How should I call a header file that can be included from both C and C++? That is, it contains #ifdef __cplusplus and extern "C" blocks that make its behavior identical in both languages. Is that a C header, a C++ header, or what? – Calmarius May 11 '22 at 13:30
109

There has to be a reason why these terms come together so often. While you should not tell your C teacher that his language is a subset of C++, there is some truth here. Others already have exposed your teacher's point of view. This is very nice (and illustrated with examples, etc.). But we don't live in an ivory tower, or a book.

Your big boss could not care less about the exact language you used. If he knows a bit about programming, just tell him you used C/C++ and it will sound like "I used a language that needs to be compiled to machine code, with DLLs and all the complicated stuff". This is the "external communication" part.

If you create a library that can be interfaced by both C and C++, you definitely want to call it a C/C++ library. Of course, someone will raise a hand and ask why you don't call this a C library that happens to have a C++ wrapper, and anyway C++ can link to C libraries so you don't need to mention it at all. Just answer: "Yes, you are right, this is a C/C++ library". This is the "internal communication" part.

If you create a lexical analyzer for C++, you'd be surprised how well it works with C. You might even not need to modify it all. This is the "if it looks like a duck, etc." part.

Etc.

The majority of C programs I have ever seen compile (and work) without modification as C++ code. Don't let a few exceptions or dogmatic (however influential) programmers fool your intuition. C and C++ are so close, and so often compatible, and so often mixed and matched together, that the term C/C++ is used. It is used because it is useful to describe these kinds of situations where it does not really matter whether you are considering C or C++, as long as it's not Java or PHP. We know it's "wrong", but we don't care, it's more useful than wrong.

It may be abused, it may be stupid, but still, I'm not sure what benefit you will get by being more pedantic than needed and refuse to communicate in terms that others understand. If you feel uneasy in some specific situation, then simply don't use the generic term C/C++, but the one relevant to the case (either C or C++).

Don't be afraid of the future. Our operating systems are written in C. Quite a lot of the C/C++ current software production happens in C++. This couple is here to stay for quite a while. Nobody has interest in one being made more incompatible with the other (quite the other way around, actually).

To be specific about your points:

1) It depends. Yes when it could lead to a confusion, when you feel uneasy, or when it's simply wrong or out of context. No when you think it's adequate.

2) N/A

3) I think no, but I have no crystal ball.

4) no idea

5) I don't think so as nothing pushes in this direction

  • 7
    Comments removed as they were getting noisy. – ChrisF Oct 01 '15 at 19:52
  • 3
    Why this has so many upvotes? It is full of inaccurateness and false claims! – Zaibis Oct 05 '15 at 13:07
  • 5
    @Zaibis would you be kind enough to expand on which claims are false or inaccurate? – usernumber Oct 05 '15 at 14:10
  • 1
    Your answer would be better without _"There __has__ to be a reason why these terms come together so often."_. Facts as a substitute for reasoning would lead to flat earths. – phresnel Oct 05 '15 at 15:52
  • 2
    @phresnel: Saying that there "has to be " a reason that people do something doesn't necessarily mean that people must have a good reason for doing it. It does, however, often suggest that it's *likely* that people would have a good idea for doing it, and one should be loath to believe that people don't have a good reason for doing it unless one can find an alternative reason for people's actions. – supercat Oct 05 '15 at 21:57
  • 3
    Following your logic, C/C++/Objective-C/Fortran/Pascal should be a valid expression but this sounds silly. The problem with the term C/C++ is that it can mean several things so you have no guarantee it will convery the desired meaning. In my practice, people usually understand it as "C is just an old primitive version of C++" and this is just wrong. – martinkunev Oct 05 '15 at 22:05
  • @usernumber: I can state my points, sure. Well the library stuff is technically ok. But I would argue with the same the compleet opposite. The lexical analyzer is simply not correct. there are many things that are catched by a C++ analyzer the same way as they are meant by C but just write a C-code containing an declaration like this `int new;` and the analyzer would catch it as something it is not meant to be by C. I can understand that you might call this fussy. But the point is a Java lexical analyzer would also theoretically be able to catch up stuff like `{}`,`for`,`while`,`;` in the same – Zaibis Oct 06 '15 at 07:43
  • But this wouldn't give any evidence that one is a subset of the other. And this is the point where it is simply wrong! C isn't a subset of C++, BECAUSE: The C++ standard says it is implementing the behaving as given in the C standard, IF NOT STATED DIFFERENT. And this notes about things that are different behaving appear not so rare as the poster here probabbly belives. and that is the reason why they are different at all. C++ implements a some here and there changed version of C. That makes it strictly seen incompatible with plain C! – Zaibis Oct 06 '15 at 07:44
  • And the point why I'm advising anyone to differ between c and c++ and don't naming something "C/C++" if not by given semantic reasons that would explicite require this term(So where calling it `C`, `C++` or `C and C++` would be incorrect) is: People are asuming exactly that there are no comatibility problems between C and C++, even more if they started developing on windows in MSVS as the MSVC compiler is mixing C and C++ so hard and even cuts off features of booth languages which have to be supported by standard, – Zaibis Oct 06 '15 at 07:44
  • in such a way that I even avoid calling the language compiled by MSVC-compiler neither C nor C++ as it is nothing of both. And people learned writing their C or C++ programms in that way, claim to have written portable code without knowing all the nit-picky stuff their code does in all the different versions. and some of them probably didn't even ever hear of unspecified or undefined behaving not asking about the difference. But the point is, as a professionel in this buisiness you have to be that nit-picky to do a clean job. – Zaibis Oct 06 '15 at 07:44
  • And I could never trust some one in this buisiness to do such a clean job if he tells me something about his projects, done in `C/C++`. (as long this statement doesn't indicate in anyway that he is aware of the difference and uses this just as a kind of word play.) – Zaibis Oct 06 '15 at 07:44
  • Sorry for this wall of text, I wanted to avoid spamming it, but I got explicit aksed for it. – Zaibis Oct 06 '15 at 07:45
  • @Zaibis: all your points have been stated already by other answers - I am merely offering a different point of view. If you don't agree, fine, downvote and move on (And please leave your rants about MSVC and brainless peers where they belong, i.e. not here). This answer has already been trolled once (and hopefully cleaned up), please don't let this happen again (write your own if you need to, downvote otherwise). Thanks. –  Oct 06 '15 at 09:13
  • I just stated what I was asked for. I wasn't intending to do so. And it isn't a rant at all, I'm not saying that there is no use for the MSVC stuff. But its a fact that it can't be C nor C++ by their definitions. And its also a fact that it gives someone, who's learning to programm a wrong impression about what C or C++ is. And you also can't deny if you were by your self allready in such an area, that if it comes to portability, that what you called "brainless peers" is something which is simply required when working with C or C++, as it otherwise tends to cause a lot of portability trouble. – Zaibis Oct 06 '15 at 09:59
  • Thats just what I wanted to explain. And there was no intention of starting a rant. – Zaibis Oct 06 '15 at 10:05
  • 4
    Beyond the intrinsic validity of the term, `C/C++` is not a clear one, and thus not useful. Following your examples, a `C/C++ library` could either mean a C library with a C++ wrapper, or a C++ library with a C wrapper. Or it could mean that the library is composed of several modules, some compiled using C++, some compiled in C. This is not clear at all. To me, saying that a library is made in `C and C++` is a lot more clear, or `C library with compatibility with C++` is also a lot more clear. And here the ambiguity is with only two lang, imagine using this "communication strategy" for more... – gaborous Oct 06 '15 at 15:02
  • Should *exposed* be *espoused* in the first paragraph? – TRiG Mar 14 '18 at 22:23
44

Going against the flow I would say it depends on the context.

The term "C/C++" is usually not appropriate when saying something like "this is a C/C++ program", but this has been explored to depth in other answers.

However, there might be contexts where C/C++ can be appropriate.

  • There are various libraries which usually have both a C and a C++ API. I guess it's not far from the truth if you call such a thing a C/C++ library. We humans like to compress information, so saying "opencv is a C/C++ library" is short, clear, and understandable, compare it to saying "opencv is a library, which is shipped with headers both for C and C++".
  • You can talk about language design and syntax. From the point of view of the language syntax, you might say that a language has C/C++ -like syntax.
  • You organize a coding contest, and you accept both solutions written in C and in C++
  • You are hiring a new programmer, and most of the tasks will be either C or C++, so the programmer is expected to know both languages. It's common in embedded development, where C is more suitable for some (usually very small) microcontrollers, and C++ for others. In this case you might say you are looking for a C/C++ programmer.
vsz
  • 1,486
  • 9
  • 17
  • 10
    I see that positions against the general consensus here on P.SO. (where you must upvote anything that looks "agile/hype/like The One And Unique Right Thing" and downvote the rest) are as welcome as usual. Guys, programming isn't a religion, and this web site is not a sacred book. –  Oct 01 '15 at 07:50
  • 16
    The last bullet point actually showcases a major problem: Are you looking (1) for a person that knowns *either* C or C++, (2) a person that knows *both* C and C++ or (3) a person that is confused to hear these are different languages. Don't help that third pool grow. – 5gon12eder Oct 01 '15 at 07:54
  • 10
    The last bullet point would be better expressed as "programmer with experience in C and C++" if that's what you are actually looking for. A job advert titled "programmer" which under the experience needed heading lists "C and C++" or "C or C++" is not significantly different from one titled "programmer" and lists "C/C++" as a necessary qualification, but it is *much* more exact. The type of person you are looking for for such a job might very well really appreciate that exactness of expression. – user Oct 01 '15 at 10:58
  • Or, you can get a c++ programmer to write a program for your microcontroller, if there is a c++ compiler. – BЈовић Oct 01 '15 at 12:07
  • 10
    In written language, the '/' it is usually interpreted as a logical `or`, not as a logical `xor`. So it's either one or both. In job listings you know either C or C++ or both. Now, I feel like a lot of people are being religious about the term. A *good* C programmer will be perfectly fine picking up C++ independently of how different both languages are. I started coding PHP and then I moved to Scala (two completely different languages) Why would a C programmer not being able to pick up C++ or viceversa? The C/C++ term has some merit if used within the right context. – ILikeTacos Oct 01 '15 at 16:51
  • 4
    You could also use C/C++ to describe a program that is intended to work when compiled as either C or C++, or a program with separate C and C++ components. – user253751 Oct 01 '15 at 21:05
  • 4
    @Tibo Programming isn't a religion? Blasphemy! ;) – Wayne Werner Oct 02 '15 at 15:54
  • "_various libraries which usually have both a C_" Other languages can expose a C API, and people don't say C/Pascal or whatever. People only say C/C++ because C++ initially (C with classes, in the 80s) was a C extension. – Trinidad Oct 04 '15 at 21:03
31

In general the SO users ask the person who is asking the question to choose a language: C or C++. Why?

There are many subtle differences between C and C++. For example, in C++, a const variable at global scope has internal linkage unless declared extern, but in C it has external linkage unless declared static. By saying "C/C++", the OP is asserting knowledge that the answer to their question is the same in both C and C++, when it very well might not be. This needlessly makes things harder for would-be answerers.

  • Sometimes we can spot that the code isn't valid in one language or the other (for example, implicit conversions from void* to pointer to object are not valid in C++). This is annoying. Why are you saying "C/C++" when you have a piece of code that's valid in C but not C++? Did you intend C, or is this just an error in code intended to be C++?

  • Sometimes the answer will be different depending on the language (for example, variable-length arrays exist in C99 but not in C++). If we don't know what language you're talking about, either we have to guess, or write an answer for both when only one will actually be useful, because you know which language you're actually using; you're just not telling us!

  • Sometimes the answer really is the same for both languages, but it's hard to be sure. For example, I think C and C++ have the same integer conversion rules, but in order to be really, really sure, I have to read both standards carefully. Again, this makes me do twice as much work as necessary when you probably only care about one of the languages.

Anyway, to answer your other questions:

  1. Yes.

  2. If you are linking together C and C++ code, it is acceptable to use both tags, but please specify which language each file is in.

  3. There are breaking changes sometimes, but they're rare and typically limited in impact (otherwise they don't get approved). For example, auto in C++11.

  4. I don't think they directly collaborate, but they pay attention to developments in the other language and try to avoid introducing changes that would make compatibility more difficult.

And if you really do want to know about both languages, that's fine, and you can say that in your question. When you say "C/C++", I'm really not sure what you mean, and it really looks like you're making an assumption about the two languages.

Brian
  • 617
  • 5
  • 7
  • 7
    I know that there are people who write code in the intersection of C and C++, and use a C++ compiler for typechecking, but a C compiler for code generation. I have no idea whether that actually makes sense or not, though. However, it is a topic that comes up every couple of years on the Linux Kernel Mailinglist, when someone submits a patch to rename the (very important in the Linux Kernel's Unified Object-Oriented Driver Model) `struct class` to something like `struct klass` for exactly that reason, and then gets invariably shot down by Linus. – Jörg W Mittag Sep 30 '15 at 21:36
  • 3
    @JörgWMittag: I have never met anyone who used "C/C++" as a a shorthand for "the common subset of C and C++" and also knew what he was talking about. People who are intentionally working in the common subset tend to make that explicit by not abbreviating. – Bart van Ingen Schenau Oct 01 '15 at 07:01
  • 3
    This is a good answer specifically about the use of C/C++ *in Stack Exchange questions*, rather than in general. – user253751 Oct 01 '15 at 21:15
  • @BartvanIngenSchenau How many std committee members have you met? – curiousguy May 09 '19 at 04:34
17

I was always told that C is a subset of C++ or C++ is C with classes. And that was quiet true until the appearance of C++x0, C++11 (or the modern C++ 11/14/17 in general).

C has never been a subset of C++. For example C89 is not a subset of C++98.

A few examples:

  • the C89 identifier-list form for function parameter declaration is not supported in C++
  • C89 and C++98 have different types for the characters constants
  • C89 and C++98 have different types for string literals
  • logical operators yield different types in C89 and C++98 (int vs bool)
  1. Should I stop using the term C/C++?

Yes.

  1. If the answer to #1 is yes, how would I call a program that use a mix of C and C++?

A program is either C or C++ (if even some very basic program can compiled with either a C or a C++ compiler). What compiler are you using to compile it? It should answer your question. Harbison & Steele coined the term Clean C to designate a common subset of C and C++ but I think it was a bad idea.

EDIT: However I admit that technically you can link C and C++ objects files in a single program but OTH there are many languages that are allowed to be mixed in a single program for example Java and C++. I think using the term C/C++ program only adds to the confusion that it is written in a single language called C/C++.

  1. Given that both of them are 'different' languages is it likely that at some point C++ compilers stop supporting code written in the C language (since modern c++ is diverging from the C mentality for basic stuff like pointers, dynamic memory handling, etc)

There are many features (example: variable length array, flexible array member, _Generic, ...) of C99 or C11 that are not supported by any C++ version.

ouah
  • 287
  • 1
  • 3
  • Re multipl- language programs: a program using JNI extensively could be called a C/Java program. The fact that two languages are used together doesn't mean they're compiled together. – user253751 Oct 01 '15 at 18:52
  • 1
    http://www.stroustrup.com/bs_faq.html#C-is-subset "In the strict mathematical sense, C isn't a subset of C++...However, C++ supports every programming technique supported by C... It is not uncommon to be able to convert tens of thousands of lines of C to C-style C++ in a few hours. Thus, **C++ is as much a superset of ANSI C as ANSI C is a superset of K&R C and much as ISO C++ is a superset of C++ as it existed in 1985**. Well written C tends to be legal C++ also. For example, every example in Kernighan & Ritchie: "The C Programming Language (2nd Edition)" is also a C++ program." – Ben Oct 02 '15 at 09:01
  • @Ben the first program (hello world) in "The C Programming Language (2nd Edition)" omits the return type of main which is invalid in C++. – ouah Oct 02 '15 at 10:19
  • 1
    @ouah, It looks like Professor Stroustrup missed one then :-) Note that's not allowed in C11 either :-) Pretty sure it was allowed in earlier versions of C++ though. – Ben Oct 02 '15 at 11:11
  • @Ben this program is not allowed in any C++ versions (including the first C++ version, C++98). – ouah Oct 02 '15 at 12:08
  • 5
    @ouah, That's not the first version of C++. The book is from **1988** and at that time neither language was an ISO standard. The current version of C++ at that time was Bjarne Stroustrup's 1985 book. – Ben Oct 02 '15 at 12:30
  • @Ben by the *the first version* of C++ I meant the first version of Standard C++. – ouah Oct 02 '15 at 12:45
  • @ouah C++ existence did not began in 98. (Note that even the std text of C++98 predates 98, as it was voted in 97.) – curiousguy May 09 '19 at 04:38
17

Some programs are written in a mixture of C and C++

This is just a fact of life. You can compile object files from C and C++ and link them together. The result can quite reasonably be called "a C/C++ program".

But that's only the program as a whole. What about the individual compilation units?

There is a subset of C which is also a subset of C++

A program (or compilation unit) written in that subset will compile and behave the same under conformant C and C++ compilers. Such a program or file can rightly be called "a C/C++ program" or "a C/C++ file".

A partial program such as a header file may also be used in both C and C++ programs. Such header files can rightly be referred to as C/C++ headers.

Quoting Professor Bjarne Stroustrup:

Is C a subset of C++?

In the strict mathematical sense, C isn't a subset of C++. There are programs that are valid C but not valid C++ and even a few ways of writing code that has a different meaning in C and C++. However, C++ supports every programming technique supported by C. Every C program can be written in essentially the same way in C++ with the same run-time and space efficiency. It is not uncommon to be able to convert tens of thousands of lines of ANSI C to C-style C++ in a few hours. Thus, C++ is as much a superset of ANSI C as ANSI C is a superset of K&R C and much as ISO C++ is a superset of C++ as it existed in 1985.

Well written C tends to be legal C++ also. For example, every example in Kernighan & Ritchie: "The C Programming Language (2nd Edition)" is also a C++ program.

So yes there is such a thing as C/C++. It's anything which is both valid C and valid C++.

The C pre-processor is part of the C language. The C++ pre-processor is part of the C++ language

You can write a compilation unit which will compile under C or C++ and be different. For example, it might have basic functionality compiled in C but take advantage of a C++ library if compiled in C++.

If the program is essentially the same, but with additional features, it's not exactly wrong to say it's the same program. Its the same, but also different.

Most C programmers can do at least a little C++ and vice-versa

It's not unreasonable to call such a person a C/C++ programmer. Yes, they probably specialise in one, but is there anyone who is a competent C or C++ programmer who literally cannot do any of the other language? In a way, aren't they all C/C++ programmers?

There's nothing wrong with saying "C/C++". What matters is being understood

The English language is not a tool for expressing syllogisms. You can use English for logic, but only just, and with great effort.

This is because words do not naturally have exact meanings, but rather a vague cloud of denotations and connotations. What matters is if people understand what you are saying.

Ben
  • 853
  • 2
  • 6
  • 9
  • 2
    This answer is so wrong. It is either C or C++, but it can't be both. People who say "C/C++" are people who do not understand the difference, and who are incompetent in both. – BЈовић Oct 01 '15 at 12:05
  • It's true that C and C++ share a common subset. But then, again, so do C and C#, C++ and C#, C and Java, and so on. – Theodoros Chatzigiannakis Oct 01 '15 at 13:06
  • 1
    Regarding first paragraph, what about web applications? Can we call them e.g. Python/JavaScript? – el.pescado - нет войне Oct 01 '15 at 13:21
  • 5
    @BЈовић They disagree with you about the acceptability of a small piece of terminology so you conclude they don't understand the difference and are incompetent. That's a pretty unreasonable thing to say. – Ben Oct 01 '15 at 13:46
  • 3
    @TheodorosChatzigiannakis C and C# do not have a subset which is a complete language. One cannot write a program which compiles both as C# and as C without using conditional compilation. With C and C++ you have all the functionality of C available if you add a few casts and ensure you declare function signatures and things like that. – Ben Oct 01 '15 at 13:53
  • 4
    @el.pescado Yes. You will find that people do in fact refer to PHP/JavaScript. Why not? The point is to be understood, not to play games with words. – Ben Oct 01 '15 at 13:53
  • @Ben People who are creating "C/C++" libraries tend to put various c++ keywords in headers (class, protected, new, ... ), making then C only. If you look at the languages, they are completely different : one is functional, other object oriented with functions. And yet there are incometent people who think they can use both in one program, although that is not possible. Now, is it play with words? No, it is not. Those people really think that the language is C/C++. Would you call OpenGL a C/C++/Python library? – BЈовић Oct 01 '15 at 14:08
  • 10
    @BЈовић You are making no sense. They are not "completely different". C++ is a much *larger* language than C, but it includes almost all of C within it. There are incompatibilities which prevent C++ being a *proper superset* but they are small, like additional reserved words, a few more casts become necessary, but that is pretty much it. It is *almost* a proper superset, just not quite. – Ben Oct 01 '15 at 14:15
  • @Ben "almost"? "just not quite"? You claimed above that there is a such nonsense as "C/C++". Look at examples in the accepted answer - there are more. The fact that c++ provides classes, templates, exceptions, standard library and rest of things, makes it quite different from C. – BЈовић Oct 01 '15 at 15:02
  • 7
    @BЈовић Perhaps you did not understand what I wrote. Why don't you look here to see how in every case it is possible to adapt a C program so that it also compiles as C++. http://david.tribble.com/text/cdiffs.htm#C99-vs-CPP9 – Ben Oct 01 '15 at 15:22
  • 7
    @BЈовић If you're one to be pedantic about terminology, you should be aware that C is not "[functional](https://en.wikipedia.org/wiki/Functional_programming_language)", but rather "[procedural](https://en.wikipedia.org/wiki/Procedural_programming_language)" -- a difference you may wish to understand before you sling accusations of incompetence at others. – R.M. Oct 01 '15 at 16:25
  • @R.M. I can live with that, as I never programed in C, except in high school ;) – BЈовић Oct 02 '15 at 08:56
  • 2
    Nice, balanced answer. However, I would disagree with Stroustrups assertion that "well written C tends to be legal C++ also". If only for ubiquitous `Foo* bar = malloc(sizeof(*bar));` in a well written C-program (note that there is no cast, which would be required in C++, but which is contraproductive in C). Use of true VLA support in C is another point where well written C is most certainly not valid C++. – cmaster - reinstate monica Oct 02 '15 at 18:38
  • 2
    @cmaster: I think Stroustrup's claim was made before C99 added VLAs, and I favor `someVar = (someType*)malloc(sizeof (someType));` since (1) the typecast will not prevent any decent compiler from squawking if no prototype for "malloc" is in scope, and (2) if `someVar` is a pointer to the wrong type, the above code will squawk. Without the typecast, it would compile but likely fail if the types are different sizes; further, even if the types are the same size now, changing the size of either type would make the code break. On the other hand... – supercat Oct 02 '15 at 19:34
  • ...there are many cases where it makes sense for well-written C code to perform arithmetic on "enum" types [e.g. one might have many cases in a switch statement increment the state variable] but I don't know any way to declare a variable in C++ such that a debugger will regard it as an enumerated type and show its value symbolically, but it can be operated on without typecasts as though it were an integer type. – supercat Oct 02 '15 at 19:38
  • 1
    @supercat One more note on the `malloc()` call: The possibility of type mismatch is the reason why I used `*bar` (i. e. the dereferenced pointer that is being allocated) in the `sizeof()` expression instead of the plain type. This assures that the type of which the size is taken matches the type of the pointer, making the `malloc()` call almost as safe as the corresponding `new` statement. Unfortunately, one sees a lot of the inferior `malloc(sizeof(someType))` in real code, though. – cmaster - reinstate monica Oct 02 '15 at 21:28
  • @cmaster: For situations where the thing being assigned is a simple variable, using that as the sizeof argument is fine, but if it's being assigned to `myArray[i*16+j]`, `foo->data`, etc. duplicating the target lvalue can be irksome. Further, the fact that C has two good ways of writing malloc() which are pretty well equivalent and C++ only likes one of them doesn't violate Stroustrup's claim nearly as strongly as does the C++ inability (so far as I can tell) to declare enums that support the standard pre-defined arithmetic operators. – supercat Oct 02 '15 at 22:19
14
  1. Should I stop using the term C/C++?

Absolutely. It is not clear what this construct is intended to express except, perhaps, confusion about what C and C++ are on behalf of the person who uses the term.

Since this confusion is such a common source of frustration, many people have become quite emotional about it and appearance of that term alone will be reason enough for them to become negative about your contribution. This might seem silly but it appears to be what we have.

I recommend that instead of talking about “C/C++” you use a term that actually makes clear what you mean.

  • If you are talking about something in C that might or might not also be true for C++, simply say C.

    Example: How should the main function be declared in C?

    At first, it might seem that the answer for C++ is the same: int main() or int main(int, char**). But as the discussion goes on, it might be relevant to point out that in C++, the function has to be declared at global scope, which doesn't make sense in C, because it has no namespaces. On the other hand, C allows calling main recursively while C++ doesn't. In C++, there is an implicit return 0; if you “fall off” main but in C the return statement is required on any path. The list goes on and it makes the discussion much simpler if you make it clear up front what the language to be discussed is.

  • If you are talking about something in C++ that might or might not also be true for C, simply say C++.

    Example: Will a malloc()ed array of ints initially be all-zeros in C++?

    The short answer for C happens to be the same: no. But as the answer goes on, it might be worthwhile to point out that in C, calloc would be a good alternative while in C++, using a std::vector<int> might have been a better choice in the first place.

  • If you want to point out a similarity between C and C++, say C and C++.

    Example: In C and C++, the sizeof an int is implementation defined and may vary between compilers and architectures.

    Here, we want to point out that C and C++ behave the same way. We are explicitly talking about both languages.

I actually recommend that you be even more specific and not only talk about “C” or “C++” but the precise version. Both languages are evolving and a blunt statement such as

C++ supports /* … */ and // … comments while C only supports the /* … */ style.

is neither right nor wrong.

  1. If the answer to #1 is yes, how would I call a program that use a mix of C and C++?

Since the languages have overlap, every C program will contain parts that might look like C++ and vice versa. Nevertheless, the authors will probably have settled onto using either a C or a C++ compiler. So say “the program is written in C” if it is compiled with a C compiler and “the program is written in C++” if they use a C++ compiler, even if they might refuse to use any modern C++ features. Some people refer to such C++ code as C-style C++. Absence of overloading, exceptions, polymorphism, templates and I/O streams a common characteristics of such code.

If, instead, some files are written in C and compiled with a C compiler and some other files are written in C++ and compiled with a C++ compiler, and then the object files linked together, I would say that “the program is written in a mix of C and C++” as, in fact, you already did.

However, if, instead, the authors took great care to write each and every file in such a way that it can be compiled with a C or a C++ compiler and the resulting program would do the same thing, you could say that “the program is written in a common subset of C and C++”.

The latter is often the case for header files that should be shared between C and C++ code. Writing such code is not easy, by the way. If you want to further emphasize that only such constructs were used that are valid in C and C++ and are widely supported by different compiler vendors, the term a portable common subset of C and C++ may be used to emphasize this.

  1. Given that both of them are “different” languages is it likely that at some point C++ compilers stop supporting code written in the C language (since modern C++ is diverging from the C mentality for basic stuff like pointers, dynamic memory handling, etc)?

I'm not sure I understand this question. Since C and C++ are different languages, you cannot expect a compiler for one of them accept a program written for the other. However, compilers are often designed in a modular way and if a compiler has a C++ front-end, chances are good it will also have a C front-end. (You would then select which of them you want via a command line switch or similar means.) As long as both languages will be in widespread use, it seems very unlikely that this is going to change. Your point about “modern C++” I think is basically a matter of good coding standards and the standard library. From the compiler's point of view, the evolution of both languages is rather converging than diverging.

  1. Is there right now any collaboration between the people who make the standards of C/C++ to keep the compatibility?

Yes. The memory model and the atomic operations library introduced in C++11 and C11 is a good example. It seems that the designers of both languages realize that compatibility is important and are working towards improving it. Personally, I'd wish that the collaboration were more intense and the two ISO working groups perhaps even joined but my wishes are not important.

Bjarne Stroustrup talks about the differences and commonalities between the various versions of C and C++ in § 44.3 of the 4th edition of The C++ Programming Language which, ironically, is titled “C/C++ Compatibility”. Use of the term might actually be appropriate in this case as it is clear what is meant.

  1. If #4 is yes, such collaboration could end up in the near future with the appearance of the modern C++ (11/14/17)

As discussed above, it happened in C++11 and is expected / hoped / needed to happen again.

5gon12eder
  • 6,956
  • 2
  • 23
  • 29
  • How can it be neither right nor wrong? – JDługosz Oct 01 '15 at 07:48
  • 4
    It doesn't have a well-defined truth value as is the case with the statement that “green things are expensive”. For a particular combination of versions of C and C++ it is true, for others it is false. – 5gon12eder Oct 01 '15 at 07:57
5

C/C++ is the intersection of C and C++.

int new; is not C/C++, and neither is vector<int> foo;.

Similarly, C89/C99 is the intersection of these two languages, where neither enum bool { false, true }; or for(int i = 0;;) is allowed.

And C++11/C++14, etc.

It is possible to write code that compiles (and correctly runs) under C++11 and C++14, even though compiling under one doesn't imply it compiles under the other. In fact, a lot people do this.

And a lot of people write code that works in C and C++.

Obviously, the greater the overlap, the more sense it makes; I don't expect to see any questions about C/C++/Java code.


Though it does "make sense" to talk about a common subset of these languages, many questions will not have answers in this subset, e.g. What should main() return in C and C++?

But you can talk about code that works for multiple language specifications, whether those specs are differentiated by "version" or "language name" or otherwise.

Paul Draper
  • 5,972
  • 3
  • 22
  • 37
3

This is a response of sorts to the position that "this is a code for programmers who work close to the metal and is OK in a management context" seen in some of the other answer and comments.


I'd argue that even that interpretation should be taken with care.

Starting by at least the mid '90s if you wanted a C++ programmer and someone who described themselves as a C programmer applied you'd have had to ask how much they know about object oriented design, how much experience they have with debugging in an object oriented context, and about their ability to use template libraries. You'd want to probe exactly those issues during the interview and hiring process.

On the flip side, it has now been more than a decade since C++ gurus started pushing "modern C++" meaning a emphasis on moving away from bare pointers to safer pointer objects and iterator-based idioms. With the emergence of C++11 there is now explicit support for multi-paradigm programming and the push toward code that exhibits no bare pointers is very strong. What that means is that if I interviewed a C++ programmer for a C position today I'd be very concerned about checking how familiar this person was with actual, foot-shooting-enabled pointers.

I'm not in the business these days (even to the degree that I was when Stack Overflow was in its infancy), so I won't venture a guess at how often either imaginary interviewee would not have the cross-over skills, but I think that as most often applied the languages are now very different indeed.

In short "C/C++" should be dropped not only in technical contexts but in most business contexts as well.

  • A good way of pointing out that the ambiguity of whether it means "either C or C++, who knows", "C and C++, linked together", "C and C++, the intersection", or "C or C++, who cares" is increasingly less justifiable, even where one could get away with it somehow. – Deduplicator Oct 04 '15 at 22:24
1

The most simple answer to this question is that you should never have used that term. It is a term that should not exist. It has no meaning. Every program is either C, or C++.

And that was quiet true until the appearance of C++x0, C++11 (or the modern C++ 11/14/17 in general).

C++98 and 03 aren't even remotely C with Classes either. Whoever taught you this doesn't know shit and you can forget them. This was never correct.

DeadMG
  • 36,794
  • 8
  • 70
  • 139
  • This is not entirely true. There was at one time a page that described a proposed language spec for a language called "C/C++" that, among other things, specified the (near) absence of a type system. Sadly, the page has since been taken down, and the site that hosted it has since put up a robots.txt that erased the archived copy from archive.org. – greyfade Oct 01 '15 at 06:34
  • 1
    Aha, I've found a copy on archive.is: [Rationale](http://archive.is/1uyXe), [Syntax and Semantics](http://archive.is/ejd9U). – greyfade Oct 01 '15 at 06:37
  • 5
    "Every program is either C, or C++." I disagree. It's quite common to take a C program, and port it to C++ *one file at a time* (by changing a compiler option for that one file). Some files might never be ported and stay C forever. Such a program is written in both C and C++ – nikie Oct 01 '15 at 09:42
  • Not really. That program is composed of subprograms, each of which is either C or C++. Each TU, which is the whole program as far as the compiler is concerned, is compiled as either C or C++. – DeadMG Oct 01 '15 at 16:17
  • 3
    It's not the case that "Every program is either C, or C++". Prof. Bjarne Stroustrup says: 'Well written C tends to be legal C++ also. For example, every example in Kernighan & Ritchie: "The C Programming Language (2nd Edition)" is also a C++ program.' http://www.stroustrup.com/bs_faq.html#C-is-subset – Ben Oct 01 '15 at 19:16
  • Those programs are C programs. The fact that they are incidentally also valid C++ is irrelevant- if they even were for any C++ Standard. – DeadMG Oct 01 '15 at 20:08
  • Well written C is legal C++, but i wouldn't call it well written C++. – el.pescado - нет войне Oct 01 '15 at 20:19
  • Metrowerks used to ship an actual C/C++ compiler. The compiler was initially in either C or C++ mode, depending on the file name, and could be switched to the other mode using a #pragma. – gnasher729 Oct 02 '15 at 12:13
  • @greyfade The object description reminds me a bit of Common Lisp's, huh. @gnasher729 Did they not have `extern C` back then? – JAB Oct 02 '15 at 13:33
1

Conceptually, there should be no particular difficulty with designing C source files so they they can also be compiled as-is with C++. There can indeed be some significant advantages to doing this. For example, when writing code for an embedded system it is sometimes helpful to be able to test the code on a hosted PC environment. If the code compiles cleanly as C++, it's possible to have a statement like "MOTOR_ENABLE = 1;" write to a volatile I/O bit on the embedded system (compiled as C), but trigger emulation logic on the PC (compiling as C++). It would also probably be possible to design a C++ type on the PC which would behave the way a uint16_t behaves on smaller embedded systems (so that e.g. given u16 x=65533;, a compiler would have to regard the value of x*x as nine, rather than having free reign to do anything it wants), though as yet none of my emulators have included that [in part because the C++ compilers I've used haven't done anything wacky in such cases].

Unfortunately, C programmers and C++ programmers have sufficient antipathy toward each other that the languages have, over the years, evolved in compatible ways. While C89 attempted to adapt some of the more useful features of C++ (such as function prototypes) an attitude seems to have emerged that programmers who want any of the features of C++ should use C++, ignoring the fact that there are many situations where it would be helpful to be able to use some of the features of C++ (e.g. the ability to overload functions with static or static inline linkage without having to accept the costs associated with other features that one doesn't need (e.g. the name mangling associated with exporting overloaded functions).

While the intersection of C89 and C++98 is a workable language, the usable superset of later versions of C with later versions of C++ has probably shrunk rather than grown (thanks to things like the Strict Aliasing Rule) and trends favor an ever-increasing fissure.

supercat
  • 8,335
  • 22
  • 28
  • 1
    What's "C++95"? A technical report? – Ben Voigt Oct 02 '15 at 00:10
  • 1
    If you want overloading for your internal functions but no name mangling for your external ones, you could put the former into an anonymous `namespace` and declare the latter as `extern "C"`, then use a C++ compiler. – 5gon12eder Oct 02 '15 at 07:26
  • @BenVoigt: Mea culpa. I should have looked it up. The C++ version before C99. When C89 came out, it tried to make C more like C++, but C99 added a fair number of features which C++ was definitely not interested in (e.g. variable-length arrays). I don't program much in C++, but I think a few more C++ features would help C more than the divergent features have helped it. – supercat Oct 02 '15 at 14:57
  • @5gon12eder: I've never tried to link C++ code into a C project. Is it really as simple as having everything either be static/inline or `extern "C"` or are there other complications as well (e.g. static-object initialization, etc.)? Is there any way in C++ to make it so that `foo(1234)` will invoke a `foo_const(1234)` macro while `foo(x)` [where `x` is not a constant] will invoke a `foo_var(x)` function? There are many situations in embedded code where it may make sense for a "function" like `SET_PORT(port, state)` to have three-forms based upon whether port and state are both... – supercat Oct 02 '15 at 15:09
  • ...compile-time constants, port is constant but state isn't, or where neither port nor state are constants. Is there any way to accomplish that cleanly using templates or some other standard C++ mechanism, or is it only possible on compilers with gcc extensions? – supercat Oct 02 '15 at 15:16
  • I think you are looking for `constexpr` functions introduced in C++11. But it would be better to ask a question on Stack Overflow about this. – 5gon12eder Oct 02 '15 at 17:03