I am taking a class in C++ programming and the professor told us that there is no need to learn C because C++ contains everything in C plus object-oriented features. However, some others have told me that this is not necessarily true. Can anyone shed some light on this?
-
5In your professor's defense, being too specific when teaching would be harder to understand for most, and lose focus of what's important. So he is probably aware of it not being a strict superset. – Brian R. Bondy Sep 28 '08 at 03:39
-
possible duplicate of [Should I learn C before learning C++?](http://stackoverflow.com/questions/598552/should-i-learn-c-before-learning-c) – RiaD Aug 11 '13 at 14:06
11 Answers
Overview:
It is almost true that C++ is a superset of C, and your professor is correct in that there is no need to learn C separately.
C++ adds the whole object oriented aspect, generic programming aspect, as well as having less strict rules (like variables needing to be declared at the top of each function). C++ does change the definition of some terms in C such as structs, although still in a superset way.
Examples of why it is not a strict superset:
This Wikipedia article has a couple good examples of such a differences:
One commonly encountered difference is that C allows implicit conversion from void* to other pointer types, but C++ does not. So, the following is valid C code:
int *i = malloc(sizeof(int) * 5);
... but to make it work in both C and C++ one would need to use an explicit cast:
int *i = (int *) malloc(sizeof(int) * 5)
Another common portability issue is that C++ defines many new keywords, such as new and class, that may be used as identifiers (e.g. variable names) in a C program.
This wikipedia article has further differences as well:
C++ compilers prohibit goto from crossing an initialization, as in the following C99 code:
void fn(void)
{
goto flack;
int i = 1;
flack:
;
}
What should you learn first?
You should learn C++ first, not because learning C first will hurt you, not because you will have to unlearn anything (you won't), but because there is no benefit in learning C first. You will eventually learn just about everything about C anyway because it is more or less contained in C++.

- 339,232
- 124
- 596
- 636
-
24I think you /will/ have to unlearn things if you start with C. There are many acceptable C idioms that are Bad Practice in C++. Preprocessor abuse, unnecessary use of primitive pointers, struct semantics are but a few examples that come to mind. – wilhelmtell Sep 28 '08 at 05:41
-
6... And the "older" you are, the harder it is to get rid of these habits. – wilhelmtell Sep 28 '08 at 05:42
-
I believe the type casting issue (void *) can be solved with compiler options on most platforms. – Anders Sandvig Sep 28 '08 at 11:49
-
2knowing how to do things like preprocessor abuse though doesn't mean you will use it. Basically you will use the best way you know, knowing something extra won't hurt you. It will just allow you to detect it later on in someone else's code and be able to refactor it yourself. – Brian R. Bondy Sep 28 '08 at 13:44
-
-
in addition to comments on the other answer, the examples of differences (and the difference itself) are so trivial as to be inconsequential to the decision. – necromancer Jul 14 '14 at 21:15
-
I'd argue that there's no harm in tackling C *first* as long your intention is not to use it as a stepping stone to get to C++, but rather broaden your knowledge. As commonly stated, there are quite a few C 'idioms' you will *not* learn in C++ (*Ever*), and so, if for some reason you had to work with C code you'd either be lost (Not used to layout of the program), confused by syntax, or try to implement C++ features that wouldn't suit the style of C code (Procedural). All-in-all, if you *want* to learn C first, than do it (You may run into it someday!), and as they say, the more you know... – Super Cat Nov 28 '15 at 05:45
While it's true that C++ was designed to maintain a large degree of compatibility with C and a subset of what you learn in C++ will apply to C the mindset is completely different. Programming C++ with Boost or STL is a very different experience than programming in C.
There was a term of art called using C++ as a better C. This meant using some C++ language features and tools to make C programming easier (e.g., declaring the index variable of a for loop within the for statement). But now, modern C++ development seems very different from C other than a great deal of the syntax and in those cases the C legacy often seems to be a burden rather than a benefit.

- 12,550
- 5
- 33
- 47
It might be true that you don't need to learn the syntax of C if you know the syntax of C++ but you cetainly do need to learn of how coding practices are different in C than in C++.
So your professor wasn't 100% right.
In C you don't have the classes to arrange your code into logical modules and you don't have C++ polymorphism. Yet you still need to achieve these goals somehow.
although the syntax of C is to some extent a subset of C++, programming in C is not a subset of programming in C++. it is completely different.

- 76,898
- 55
- 205
- 325
Yes and no.
As others have already answered, the language C++ is a superset of the language C, with some small exceptions, for example that sizeof('x') gives a different value.
But what I don't think has been very clearly stated is that when it comes to the use of these two languages, C++ is not a superset, but rather different. C++ contains new (it can be discussed if they are better) ways of doing the basic things, such as writing to the screen. The old C ways are still there, but you generally use the new ways. This means that a simple "hello world" program looks different in C and in C++. So it is not really true that the simple things are the same in C and C++, and then you just add more advanced stuff, such as support for object-oriented programming, in C++.
So if you have learnt C++, you will need to re-learn quite a lot before you can program in C. (Well, it is possible to teach C++ as an extension to C, still using printf and malloc instead of iostreams and new, and then adding classes and other C++ things, but that way of using C++ is generally frowned upon.)

- 27,232
- 8
- 51
- 75
No C++ isn't really a superset of C. You can check this article for a more extensive list of the differences if you're interested: http://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B

- 295
- 2
- 7
-
2And there will be more compatibilities as we approach the C1x and C++1x standards. Although the committees strive for compatibility, there are already features that won't be in both languages (according to the drafts). – Kensai Sep 21 '09 at 09:21
The biggest "gotcha" is typing -- C++ is much more strongly typed than C is, and the preferred methods for solving this in C++ are simply not available in C. Namely, you can silently cast between types in C (particularly pointer types), but not in C++. And C++ highly recommends using the static_cast/reinterpret_cast/const_cast methods for resolving these issues.
More importantly, if you learn C++ syntax and mannerisms, you'll probably find it difficult to deal with C (some may say this is good; and I prefer C++ myself, but sometimes it just isn't an option, or you have to deal with legacy code that's in C and not C++). Again, the most likely issues you'll encounter are dealing with pointers (particularly char*'s and general array usage; in C++ using std::string and std::vector or other collections is simply better).
It's certainly possible to learn C++, and then learn the differences between C and C++ and be capable of programming in both. But the differences are far more than just skin deep.

- 9,948
- 2
- 25
- 22
It is true that for most purposes, C++ contains everything that C does. Language lawyers will be quick to point out that there are some very special edge cases that are valid C but not valid C++.
One such example might be the C declaration
int virtual;
which declares an integer named "virtual". Since "virtual" is a keyword in C++, this is not valid C++.

- 951,095
- 183
- 1,149
- 1,285
There is a large common core of C (especially C89) and C++, but there are most certainly areas of difference between C and C++. Obviously, C++ has all the object-oriented features, plus the generic programming, plus exceptions, plus namespaces that C does not. However, there are also features of C that are not in C++, such as support for the (close to archaic) non-prototype notation for declaring and defining functions. In particular, the meaning of the following function declaration is different in C and C++:
extern void function();
In C++, that is a function that returns no value and takes no parameters (and, therefore, is called solely for its side-effects, whatever they are). In C, that is a function which returns no value but for which there is no information about the argument list. C still does not require a declaration in scope before a function is called (in general; you must have a declaration in scope if the function takes a variable list of arguments, so it is critical to #include <stdio.h>
before using printf()
, etc).
There are also differences:
sizeof('c')
In C++, the answer is 1; in C, the answer is normally 4 (32-bit systems with 8-bit characters) or even 8 (64-bit systems with 64-bit int).
In general, you can write code that will compile under both C and C++ compilers without much difficulty - the majority of my code does that all the time. The exceptions are either a result of carelessness on my part, or because I've consciously exploited the good features of C99 that are not in C++ 98, such as designated initializers, or long long
.

- 730,956
- 141
- 904
- 1,278
Stroustrup himself advices against learning C first. But then again, he (and many others of his generation) managed to become a C++ guru starting from C.

- 984
- 12
- 20
-
3He also explains why in his 1999 paper (page 9). http://www.research.att.com/~bs/new_learning.pdf – Kensai Sep 21 '09 at 11:19
I personally would disagree with your professor.
Generally speaking, C++ is based on C and in that "sense" contains it and extends it.
However, since traditionally people learned C and only then the extensions of C++, your professor's statement is incorrect since to use C++ correctly you would need to master the C origins. It is possible that when teaching you something, your professor or textbook will not specifically mention what came from which language.
In addition, it is important to understand that despite the similarities, not every C program runs in the same way under C++. For example, C structs are interpreted differently (as classes with everything public) by the C++ compiler.
When I teach, I teach the C core first, and then go to C++.

- 88,451
- 51
- 221
- 321
-
2"C structs are interpreted differently"? As far as I can see, "classes with everything public" in C++ work exactly as structs in C, as long as you don't add any C++-specific things such as member functions. So a C program with structs works exactly the same when compiled as C++. Or am I wrong? – Thomas Padron-McCarthy Sep 28 '08 at 06:28
-
2From a pure standpoint, when you compile a struct in C++, it generates a class including default constructors and copy constructors. It used to be a question in an advanced OOP course I took years ago and then taught for a few years. – Uri Oct 17 '08 at 00:42
-
1"...to use C++ correctly you would need to master the C origins..." I'd say this is blatantly wrong. I think that _to use C++ correctly you would need to forget all your C habits_. That's very hard, which is why I _very_ strongly oppose this POV. – sbi Sep 12 '09 at 12:58
If any of the students in the class intend to become embedded software engineers, then they may have no choice but to program in C (see this question, and this one, among others).
Of course, having learnt C++, it may be less of a transition for them than starting from scratch - but it still makes your professor's statement untrue!

- 1
- 1

- 2,620
- 1
- 22
- 24
-
1
-
1Yes - but in this case, slowly. Nonetheless, not all embedded processors have C++ compilers, those that do often charge more for the C++ option, and the majority of existing code is still written in C. So I stand by my answer! – Steve Melnikoff Sep 21 '09 at 12:23