32

Is C/C++ one language or two languages? I heard C++ was just C with classes. Is that right?

fredoverflow
  • 256,549
  • 94
  • 388
  • 662
Scott O'Donnell
  • 575
  • 4
  • 6
  • 20
    That's not very nice, guys. The question could probably be answered with a google search, but you need to calm down. You really need to discern between a question asked in bad faith and a question from someone new to programming. – zneak Jan 15 '13 at 02:50
  • 1
    C with classes, templates, exceptions, overloading, operator overloading, virtual functions, lambdas, ... – Yuushi Jan 15 '13 at 02:52
  • I use "C/C++" as a shorthand for "the intersection of C and C++", which is a language that many programs and libraries are intentionally written in, so that the code can compile in both a C compiler and a C++ compiler. – Benjamin Lindley Jan 15 '13 at 02:53
  • @BenjaminLindley: Dammit. Now "C/C++" is [politically] recognised by my subconscious OCD. I really didn't want it to be. – Lightness Races in Orbit Jan 15 '13 at 02:54
  • 4
    @BenjaminLindley The formal term for that is ["Clean C"](http://stackoverflow.com/questions/9694776/what-is-clean-c-and-how-does-it-differ-from-standard-c). – Mysticial Jan 15 '13 at 02:54
  • 3
    @Mysticial: I'm not sure what makes that the formal term. It was just the term as used in some book. The name certainly doesn't make the meaning clear. If I'm writing C code that uses variable length arrays, `restrict`, and other perfectly good features, my code is not "Clean C"? – Benjamin Lindley Jan 15 '13 at 03:11
  • 1
    @BenjaminLindley The status of "formal" can be debated. But yes, if you use VLAs, it's no longer "Clean C". I write HPC libraries, and those all written in Clean C to target both C and C++ applications. – Mysticial Jan 15 '13 at 03:12
  • 3
    @Mysticial: My point was that it's a bad term, because its meaning is entirely unclear, and so should not be used to refer to "the intersection of C and C++", and certainly not be considered formal. – Benjamin Lindley Jan 15 '13 at 03:15
  • @BenjaminLindley Fair enough, since "Clean" implies that the opposite is "Dirty". – Mysticial Jan 15 '13 at 03:17
  • @BenjaminLindley: I propose the same is true of "C/C++". – Lightness Races in Orbit Jan 15 '13 at 03:19
  • @LightnessRacesinOrbit: Perhaps. But it is, in my opinion, certainly an improvement. It at least implies that it has something to do with C++, whereas "Clean C" implies nothing like that. – Benjamin Lindley Jan 15 '13 at 03:28
  • @BenjaminLindley: This is true. On the other hand, "C/C++" drops the word "Clean", which introduces an inherent dirtiness that I think we are all better off avoiding. – Lightness Races in Orbit Jan 15 '13 at 03:29
  • 15
    I think @zneak makes an important point here. This may be a beginner question that seems obvious to everyone with a bit of experience, but it is certainly not a question that will "likely solicit debate, arguments, polling, or extended discussion". IMO there is no need to close this. – Adam Lear Jan 15 '13 at 04:06
  • I agree with @Anna about the closing, but maintain that it's downvote-worthy in its current form (the major distinction for me being that I won't [read: didn't] hesitate to answer it :D) – Lightness Races in Orbit Jan 15 '13 at 04:42
  • 27
    Actually, `C/C++` is an expression (that has undefined behavior in both in C and C++, because it modifies C *and* attempts to use its value, without an intervening sequence point). – Jerry Coffin Jan 15 '13 at 05:54
  • 1
    @Mysticial: The intersection of C and C++ is **anything but** "clean". It will have nasty gratuitous casts all over the place, e.g. every call to `malloc`, because C++ has different rules for implicit type conversion due to different intended usage. – R.. GitHub STOP HELPING ICE Jan 15 '13 at 06:35
  • @BenjaminLindley: The parts that can compile in both a C and C++ compiler is just C. There are no parts of C++ that compile in a C compiler. – Martin York Jan 15 '13 at 08:33
  • 2
    I voted to close, as I do not think this question will help anybody. – Jesse Good Jan 15 '13 at 08:37
  • @LokiAstari: I'm not sure what logic you're using, but that's clearly wrong by any kind of logic that I care about. – Benjamin Lindley Jan 15 '13 at 09:04
  • @BenjaminLindley: A C++ compiler may be able to compile a subset of C. But a C compiler by definition can't compile C++ (only the part of C++ that is C). Thus the intersection of C and C++ that is compilable by both C++ and C compilers is just C (or really a subset of C). – Martin York Jan 15 '13 at 10:36
  • 3
    @LokiAstari: A C compiler may be able to compile a subset of C++, but a C++ compiler by definition can't compile C (only the part of C that is C++). Thus, by the same logic, the intersection of C++ and C that is compilable by both C and C++ compilers is just C++ (or really a subset of C++). So which is it? C? Or C++? If it's C, why? And why does the same logic not apply when you swap C and C++ in your comment? The statement is just as true. – Benjamin Lindley Jan 15 '13 at 11:55
  • @BenjaminLindley people who try to write code that is both valid c and c++ end up doing neither. Basic funtionality like sizeof(char) differ, casting rules differ - in other words the result is error prone. The only valid way to write c/c++ is using large amounts of #ifdef cplusplus .... #endif – josefx Jan 15 '13 at 12:04
  • 2
    @josefx `sizeof(char)` is 1 in both languages. What you probably meant was `sizeof('C')` which is 1 in C++ and `sizeof(int)` in C. – fredoverflow Jan 15 '13 at 12:59
  • @BenjaminLindley: Unfortunately your logic is flawed. `A C compiler may be able to compile a subset of C++` The subset is C. `but a C++ compiler by definition can't compile C (only the part of C that is C++)` This is the empty set. Thus your final assertion: `Thus, by the same logic, the intersection of C++ and C that is compilable by both C and C++ compilers is just C++ ` is **wrong**. Thus the intersection this way round is nothing. So the combination of my comment plus your comment is still just C. Maybe you should go back to maths and look up what intersection means. – Martin York Jan 15 '13 at 17:17
  • 3
    @LokiAstari: Perhaps the concept was taught differently where you grew up. Here's an image that demonstrates the "intersection" between C and C++, as I see it. http://i45.tinypic.com/15x037n.png -- Do I have something wrong there? – Benjamin Lindley Jan 15 '13 at 21:44
  • 1
    C and C++ may be two different languages, but there's no denying that there is a lot in common (which is a major reason for C++'s success) and I think that there is a very large set of questions and answers that apply to both languages. Being dogmatic about 'C/C++' being a non-existent language isn't always the most helpful attitude, even if there are many times where the 'C/C++' term makes no sense. – Michael Burr Mar 24 '13 at 09:11

8 Answers8

68

C++ diverged from C in 1982-1983, and that's a long time in computer years. But, there are many C libraries with C++ compatibility, including the C standard library itself, and a steady stream of programs are ported across from C to C++. Many C programmers only know or use the features that are compatible with C++.

They are defined by different ISO standards from separate committees. Even when they define compatible features, it is often defined in different terms.

Referring to C/C++ is about as valid as referring to Italian/Spanish. You should be careful to whom and when you use such a term. But it's true that there is diffusion of ideas in both directions, and the similarities are more than coincidence.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
  • @BoPersson Heh, I didn't really verify it. But the committees are very large, and the members in common are few. – Potatoswatter Jan 15 '13 at 12:11
  • In the early nineties, some ideas from the C++ committee were adopted by the C committee so there is some interaction with regards to language development. And this is what one would expect when taking the history of the languages into account. – HonkyTonk Jan 15 '13 at 13:21
  • @HonkyTonk In the early 2010s, some ideas from Java (range-based `for`) were adopted by C++. Languages all mix together. Probably I should mention compatibility features like C-style casts and the macro `offsetof`, but those are details. Mutual compatibility is a secondary concern, although it does get considered. But, the C standard library will always be supported by C++, so there's that… this answer could indeed be expanded. – Potatoswatter Jan 15 '13 at 14:01
  • I dare to clarify: Programing language is defined in a document. In that context, C and C++ are two different programming languages. Enter runtime and clarity is lost. Enter ABI and C is the beast that lives down below and rules them all. – Chef Gladiator Aug 01 '21 at 10:04
36

"C/C++" is precisely zero languages. It does not exist.

On the other hand, C is a language.

C++ is another language, which is kind of like C but also has classes and lots of other differences.


To be clear, @Zoidberg was spot on:

C and C++ are two completely different languages. C with Classes was the predecessor of C++, but the term is still often used for non-modern C++ (e.g. that uses raw pointers all over the place).

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
23

It is two languages. Calling C++ "C with classes" is like calling an elephant a four legged animal. It seems true till you compare it to a mouse.

There are many languages which derive from C: C++, Java, C#, JavaScript, csh, the list goes on. They are all different in many ways but they share similar syntax.

Of course C derived from B. But that is another story (and no one cares about B anymore.)

Hogan
  • 69,564
  • 10
  • 76
  • 117
12

Simple answer: two languages

They are two different languages, although almost any C code is valid (not necessarily good) C++ code.

C++ was at first thought about as "C, but with classes", but as the time passed, it differed more and more and now C code is very bad C++ code. You can learn C or C++ or both, but you usually don't mix them up (but you can).

sbi
  • 219,715
  • 46
  • 258
  • 445
KamikazeCZ
  • 714
  • 8
  • 22
4

C/C++ is two languages. C is one language, and C++ is the other. C++ is considered a 'better' C. C is procedural, whereas C++ is object oriented. C++ has a lot of improvements over C, and has a similar syntax to C.

  • 4
    I'd be careful: I imagine there's a lot of people around who consider C++ to be more of a bastard corruption of C than a "better C". Most of these are probably VB.net programmers that can't tell the difference. I'm *not* one of them. :) – Mac Jan 15 '13 at 04:18
  • 6
    C++ is not an object-oriented language. It does support object-oriented programming, but also a handful of other paradigms like procedural programming and functional programming. Oh, and you can do OOP in C, too, only that it is rather awkward. – Ulrich Eckhardt Jan 15 '13 at 06:38
  • I suppose that is a good point. I always call it an object-oriented programming language simply because in my mind, if you don't use it as OO, you are using it wrong. But you are correct. –  Jan 15 '13 at 06:45
4

They are two different languages. C++ is so named because part of it is rooted from C and compatible with C in some sense.

According to Scott Myers's Effective C++, we can view C++ as a unified language with the following 4 components:

  1. C language part, blocks, statements, preprocessor, etc
  2. Objected Oriented C++: including class, encapsulation, inheritance, polymorphism, etc
  3. Template C++: including C++ templates, metaprogramming stuff
  4. The STL.

So C++ is more powerful than C in some sense.

didierc
  • 14,572
  • 3
  • 32
  • 52
taocp
  • 23,276
  • 10
  • 49
  • 62
2

The name "C" refers to a family of related languages, some of which are formalized as international standards. These include K&R C, ANSI C, C99, and C11.

The name "C++" refers to a family of related languages, some of which are formalized as international standards. These include C++98, C++03, C++11, C++14, and the speculated C++17.

The term "C/C++" is used by many people to informally refer to the intersection of C and C++, which has been intentionally maintained by the designers of C++.

C++'s immediate predecessor was in fact originally called "C with Classes". This is detailed in Bjarne Stroustrup's 1994 book "The Design and Evolution of C++" (and also here). The name was eventually changed to "C++", largely as a courtesy to the C community because it had become too tempting for people to shorten "C with Classes" to just "C" or "new C".

There are many language features in C++ that are not in C (the reverse is true to a lesser extent). In particular, the class-based model, due to its capability to invoke implicit function calls, is so powerful that C++ has (rather organically) evolved higher-level programming styles that make the typical usage of C++ much different than that of C. The most notable examples of this are associated with the concept called Resource Acquisition Is Initialization, which is embodied in the standard classes string, vector, and shared_ptr as well as the stream classes of the standard I/O library.

Other notes:

Community
  • 1
  • 1
Brent Bradburn
  • 51,587
  • 17
  • 154
  • 173
  • 3
    *The term "C/C++" is used by many people to informally refer to the intersection of C and C++.* In my experience, most people who use the term "C/C++" are not referring to the intersection of the two languages. In most cases, it's an indication that the writer doesn't understand the relationship between C and C++, and probably would not be able to describe the "intersection" between them. In that sense, it can be a useful flag. – Keith Thompson Jun 04 '15 at 16:07
  • @KeithThompson: Maybe the people you are referring to are *trying* to "refer to the intersection" , even if they don't know precisely what that is. Just because someone can't define something perfectly, it doesn't mean that they can't attempt to discuss it. I suppose in some context "C/C++" could be used to mean "either C or C++, but not necessarily both". In that case, what they probably want is something that works with a C++ compiler, but they are fine if it also works with a C compiler (or perhaps even the reverse). – Brent Bradburn Jun 04 '15 at 16:36
  • 1
    It might depend on what they're looking for. If a recruiter says he's looking for "C/C++" programmers, he's probably looking for C programmers and/or C++ programmers, not (necessarily) for programmers able to write programs in the intersection of C and C++. – Keith Thompson Jun 04 '15 at 17:20
1

IMHO, I think the answer is that C/C++ are not one language, but two which are:

  • C Programming language
  • C++ Programming language

The C++ language is derived from the C language. C++ is the name for the C programming language with added 'classes' functionality. That means that the basic C language architecture has been enhanced to allow object oriented programming. It uses the "++" operator that means increment. C++ is an increment of C, which allows you to use procedural way or object oriented way or both of them in programming ways. C++ allows us to write code easier than C. But this does not mean that the C language is not suitable now, because we have C++. Each language is used for goal the purpose of software that is needed by a programmer. That's why ANSI made C a standard.

Reference

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
UTHM2012
  • 91
  • 5