tags:

views:

1017

answers:

18

I've seen a lot of questions around that use improperly the expression "C/C++". The reasons in my opinion are:

  • Newbie C and C++ programmers probably don't understand the difference between the two languages.
  • People don't really care about it since they want a generic, quick and "dirty" answer

While C/C++ could sometimes be interpreted as "either C or C++", I think it's a big error. C and C++ offer different approaches to programming, and even if C code can be easily implemented into C++ programs I think that referring to two separate languages with that single expression ( C/C++ ) is wrong.

It's true that some questions can be considered either as C or C++ ones, anyway. What do you think about it?

+22  A: 

C/C++ is a holdout from the early days of C++, where they were much more similar than they were today. It's something that wasn't really wrong at first, but is getting more-so all the time.

The basic structure is similar enough that most simple questions do still work between the two, though. There is an entire Wikipedia article on this topic: http://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B

The biggest fallacy that comes from this is that because someone is well-versed in C, they will be equally good at C++.

Steve Klabnik
The second biggest fallacy that comes from this is that because someone is well-versed in C++, they will be equally good at C.
Chris Young
@Chris - Definitely. We used some C++ in college but never touched C. Someone assumed that I knew C as well and laughed when I told them I didn't.
Just Some Guy
+6  A: 

The two languages are distinct, but they have a lot in common. A lot of C code would compile just fine on a C++ compiler. At the early-student level, a lot of C++ code would still work on a C compiler.

Note that in some circumstances the meaning of the code may differ in very subtle ways between the two compilers, but I suppose that's true in some circumstances even between different brands of C++ compiler if you're foolish enough to rely on undefined or contested/non-conformant behavior.

Joel Coehoorn
+2  A: 

I was under the impression that all c code is valid c++ code.

epicstruggle
This became fuzzy as C++ developed. It originally was, or at least was intended to be, but that ended up not being the case. I cant' find an exact timeline, but somewhere around C99, they diverged.
Steve Klabnik
C has a _few_ things C++ doesn't. The one of the top of my head are ... function parameters.
Joel Coehoorn
There's also a lot of code now that would _compile_ either way, but has a very subtle difference in meaning whether your using C or C++
Joel Coehoorn
Oh yeah, don't forget that sometimes compilers do wacky things that aren't necessarily standards-conforming. So maybe there are some compilers that will do this, or cross-pollinate features...Nothing says that compilers must follow a standard exactly.
Steve Klabnik
This is largely true, but there are areas where C ocde is not valid C++ code. The one that gets run across all the time is that in C a void pointer is implicitly converted to any concrete pointer type. That is an error in C++; void pointers must be explicitly cast.
Michael Burr
Valid C code: "char *new = malloc(20)". This will not compile as C++
camh
Bjarne Stroustrup *states explicitly in his book* that C++ is (originally) intended to be a superset of C. If there was a rigorous approach to enforcing a specification upon all compilers of C++, it'd probably still be true.
Tom W
A: 

Yes, it's wrong. Next one, please.

bstark
Now, that's a senseless comment.
Prog
The prevailing culture on SO does not look kindly on simple answers with no explanation.
Robert S.
I agree with the sentiment though. I've just about stopped coming here because everyone is trying to cram general programming discussion into the framework of asking vague questions they already know the answer to. That's not what the site was designed for.
smo
+7  A: 

We in our company have noticed the following curious fact: if a job applicant writes in his CV about "advanced C/C++ knowledge", there is usually a good chance that he really knows neither ;)

Alex Jenter
I just noticed that my resume contains "C/C++ (20 years)". Maybe I should change that! :)
Greg Hewgill
What should someone write if they feel they're well versed in both?
Michael Burr
Advanced Knowledge in both C and C++?
Michael Stum
+5  A: 

Yes and no.

C and C++ share a lot in common (in fact, the majority of C is a subset of C++).

But C is more oriented "imperating programming", whereas C++, in addition to C paradigm, has more paradigms easily accessible, like functional programing, generic programing, object oriented programing, metaprograming.

So I see the "C/C++" item saying either as "the intersection of C and C++" or "familiarity with C programing as well as C++ programing", depending on the context.

Now, the two languages are really different, and have different solutions to similar problems. A C developer would find it difficult to "parse/understand" a C++ source, whereas a C++ developer would not easily recognize the patterns used in a C source.

Thus, if you want to see how far the C is from the C++ in the "C/C++" expression, a good comparison would be the GTK+ C tutorials, and the same in C++ (GTKmm):

C : GTK+ Hello World: http://library.gnome.org/devel/gtk-tutorial/stable/c39.html#SEC-HELLOWORLD

C++ : GTKmm Hello World: http://www.gtkmm.org/docs/gtkmm-2.4/docs/tutorial/html/sec-helloworld.html

Reading those sources is quite enlightening, as they are, as far as I parsed them, producing exactly the same thing, the "same" way (as far as the languages are concerned).

Thus, I guess the C/C++ "expression" can quite be expressed by the comparison of those sources.

:-)

The conclusion of all this is that it is Ok if used on the following contexts:

  • describing the intersection of C and C++
  • describing familiarity with C programing as well as C++ programing
  • describing compatible code

But it would not be for:

  • justifying keeping to code in a subset of C++ (or C) for candy compatibility with C (or C++) when compatibility is not desired (and in most C++ project, it is not desired because quite limitating).
  • asserting that C and C++ can/should be coded the same way (as NOT shown by the GTK+/GTKmm example above)
paercebal
Er, I already know the differences between C and C++.
Prog
I guessed you do, but I try to have answers as "complete" as possible (without out-of-topic-ing) to support their conclusions/viewpoints. This way, a reader will have in one post all the useful information to evaluate the answer (i.e., if writting about RAII, I would at least give an example)
paercebal
+2  A: 

C/C++ often means a programiing style, which is like C and classes, or C and STL :-) Technicaly it is C++, but minimum of its advantages are used.

akalenuk
+13  A: 

Please remember that the original implementations of C++ were simply as a pre-compiler that output C code for the 'real' compiler. All C++ concepts can be manually coded (but not compiler-enforced) in plain C.

"C/C++" is also valid when referring to compilers and other language/programming tools. Virtually every C++ compiler available will compile either - and are thus referred to as "C/C++" compilers. Most have options on whether to treat .C and .CPP files based on the extension, or compile all of them as C or all of them as C++.

Also note that mixing C and C++ source in a single compiler project was possible from the very first C/C++ compiler. This is probably the key factor in blurring the line between the languages.

Many language/programming tools that are created for C++ also work on C because the language syntax is virtually identical. Many language tools have separate Java, C#, Python versions - but they have a single "C/C++" version that works for C and C++ due to the strong similarities.

Jeff B
Last time I checked, CFront produced C code as javac will produce bytecode. It was not precompilation. It would compilation of C++ code into an intermediate portable code, that is C, that was supposed then to be compiled by a C compiler.
paercebal
+4  A: 

I think it's more of the second answer - they want something that's easily integrated into their project.

While a C answer may not be idiomatic C++ (and vice versa), I think that's one of C++'s big selling points - you can basically embed C into it. If an idiomatic answer is important, they can always specify C/C++/C++ with STL/C++ with boost/etc.

An answer in lisp is going to be pretty unusable. But an answer in either C or C++ will be directly usable.

tfinniga
This is especially true when dealing with, say, Windows code. C and C++ use the same Win32 API, but getting a .NET answer would be entirely useless.
Branan
+2  A: 

Isn’t saying “C/C++” wrong?

No, it isn't. Watcom International Corporation for example, founded more than 25 years ago, called their set of C and C++ compilers and tools "Watcom C/C++", and this product is still developed and available in the open-source form as OpenWatcom C/C++

dmityugov
+1  A: 

If it is a complex question needing to write more than one function, yes, it can be wrong.

If it is just to ask a detail about sprintf or bit manipulation, I think it can be legitimate (the latter can even be tagged C/C++/Java/C#, I suppose...).

PhiLho
+1  A: 

Whoever is asking the question should write C, C++ or C/C++ depending on the question.

From what I've seen, you can write C++ code the C way or the C++ way. Both work, but C++ code that is not written C style is usually easier to maintain in the long run.

In the end, it all depends on the particular question. If someone is asking how to concatenate strings, than it is very important whether he wants C or C++ solution. Or, another example, if someone is asking for a qsort algorithm. To support different types, you might want to use macros with C and templates with C++, etc.

Milan Babuškov
+1  A: 

This was too long for a comment, so I had to make it an answer, but it's in response to Jeff B's answer.

Please remember that the original implementations of C++ were simply as a pre-compiler that output C code for the 'real' compiler.

I have a friend (who writes C++ compilers -- yes, plural), who would take offense to your first sentence. A compiler whose object code is C source code is every bit as much a compiler as any other. The essence of a compiler is that it understands that syntax of the language, and generates new code based on that. A pre-processor has no knowledge of the language and merely reformats its input.

Remember that the C Compilers which would compile the output of those C++ compilers, would themselves output ASM code would would then be run through an assembler.

James Curran
+4  A: 

Yeah, C/C++ is pretty useless. It seems to be a term mostly used by C++ newbies. We C-only curmudgeons just say "C" and the experienced C++ folks know how much it has diverged from C and so they properly say "C++".

Even if C is (nearly) a subset of C++, this doesn't really have any bearing on their actual usage. Practically every interesting C feature is frowned upon in modern C++ code: C pointers (use iterators/smart pointers/references instead), macros (use templates and inline functions instead), stdio (use iostreams instead), etc. etc.

So, as Alex Jenter put it, it's unlikely that anyone who knows either language well would say C/C++. Saying that you know how to program in "C/C++" is like saying you know how to program in "Perl/PHP"... sure they've got some significant similarities, but the differences in how they are actually used are vast.

Dan
+3  A: 

I agree. I read the C tag RSS feed, and I see tons of C++ questions come through that really don't have anything to do with C.

I also see this exchange a lot:

Asker: How do you do this in C?
Answer: Use the X library for C++.
Asker: OK, how about someone actually answer my question in C?

PhirePhly
A: 

I tend to put C / C++ in my questions.

Typically I am looking for something that I can use in my c++ application.

If the code is in C or in C++ then I can use it, so I would rather not just limit the possible answers to one or the other.

KPexEA
+3  A: 

I use that term myself, and it is because it is my style, I don't use boost, stl or some other things, not even standard C++ libs, like "cout" and "cin", I program C but using classes, templates and other (non-library) features to my advantage.

I can say that I am not a master of C, neither a master of C++, but I am really good at that particular style that I use since 10 years ago. (and I am still improving!)

speeder
A: 

Not only these two languages are different, but also the approaches are different. C++ is an OO language, while C is procedural language.

Do I have to mention templates?

Also, there are differences in C and C++ standards. If something is good in C, doesn't have to compile in C++

VJo