[ACCEPTED]-Is it true that there is no need to learn C because C++ contains everything?-c

Accepted answer
Score: 86


It is almost true that C++ is a superset 28 of C, and your professor is correct in that 27 there is no need to learn C separately.

C++ adds 26 the whole object oriented aspect, generic 25 programming aspect, as well as having less 24 strict rules (like variables needing to 23 be declared at the top of each function). C++ does 22 change the definition of some terms in C 21 such as structs, although still in a superset 20 way.

Examples of why it is not a strict superset:

This Wikipedia article has a couple good examples of such 19 a differences:

One commonly encountered difference 18 is that C allows implicit conversion from void* to 17 other pointer types, but C++ does not. So, the 16 following is valid C code:

int *i = malloc(sizeof(int) * 5);  

... but to make 15 it work in both C and C++ one would need 14 to use an explicit cast:

int *i = (int *) malloc(sizeof(int) * 5)

Another common 13 portability issue is that C++ defines 12 many new keywords, such as new and class, that 11 may be used as identifiers (e.g. variable names) in 10 a C program.

This wikipedia article has further differences as 9 well:

C++ compilers prohibit goto from crossing 8 an initialization, as in the following C99 7 code:

 void fn(void)
  goto flack;
  int i = 1;

What should you learn first?

You should learn C++ first, not because 6 learning C first will hurt you, not because 5 you will have to unlearn anything (you won't), but 4 because there is no benefit in learning 3 C first. You will eventually learn just 2 about everything about C anyway because 1 it is more or less contained in C++.

Score: 14

While it's true that C++ was designed to 15 maintain a large degree of compatibility 14 with C and a subset of what you learn in 13 C++ will apply to C the mindset is completely 12 different. Programming C++ with Boost or 11 STL is a very different experience than 10 programming in C.

There was a term of art 9 called using C++ as a better C. This meant 8 using some C++ language features and tools 7 to make C programming easier (e.g., declaring 6 the index variable of a for loop within 5 the for statement). But now, modern C++ development 4 seems very different from C other than a 3 great deal of the syntax and in those cases 2 the C legacy often seems to be a burden 1 rather than a benefit.

Score: 7

It might be true that you don't need to 11 learn the syntax of C if you know the syntax 10 of C++ but you cetainly do need to learn 9 of how coding practices are different in 8 C than in C++.

So your professor wasn't 100% right.
In 7 C you don't have the classes to arrange 6 your code into logical modules and you don't 5 have C++ polymorphism. Yet you still need 4 to achieve these goals somehow.
although 3 the syntax of C is to some extent a subset 2 of C++, programming in C is not a subset of programming 1 in C++. it is completely different.

Score: 5

Yes and no.

As others have already answered, the 25 language C++ is a superset of the language 24 C, with some small exceptions, for example 23 that sizeof('x') gives a different value.

But 22 what I don't think has been very clearly 21 stated is that when it comes to the use of 20 these two languages, C++ is not a superset, but 19 rather different. C++ contains new (it can 18 be discussed if they are better) ways of 17 doing the basic things, such as writing 16 to the screen. The old C ways are still 15 there, but you generally use the new ways. This 14 means that a simple "hello world" program 13 looks different in C and in C++. So it is 12 not really true that the simple things are 11 the same in C and C++, and then you just 10 add more advanced stuff, such as support 9 for object-oriented programming, in C++.

So 8 if you have learnt C++, you will need to 7 re-learn quite a lot before you can program 6 in C. (Well, it is possible to teach C++ as 5 an extension to C, still using printf and 4 malloc instead of iostreams and new, and 3 then adding classes and other C++ things, but 2 that way of using C++ is generally frowned 1 upon.)

Score: 4

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

Score: 4

Not entirely true.

The biggest "gotcha" is typing 23 -- C++ is much more strongly typed than 22 C is, and the preferred methods for solving 21 this in C++ are simply not available in 20 C. Namely, you can silently cast between 19 types in C (particularly pointer types), but 18 not in C++. And C++ highly recommends using the 17 static_cast/reinterpret_cast/const_cast 16 methods for resolving these issues.

More 15 importantly, if you learn C++ syntax and 14 mannerisms, you'll probably find it difficult 13 to deal with C (some may say this is good; and 12 I prefer C++ myself, but sometimes it just 11 isn't an option, or you have to deal with 10 legacy code that's in C and not C++). Again, the 9 most likely issues you'll encounter are 8 dealing with pointers (particularly char*'s 7 and general array usage; in C++ using std::string 6 and std::vector or other collections is 5 simply better).

It's certainly possible to learn 4 C++, and then learn the differences between 3 C and C++ and be capable of programming 2 in both. But the differences are far more 1 than just skin deep.

Score: 3

It is true that for most purposes, C++ contains 7 everything that C does. Language lawyers 6 will be quick to point out that there are 5 some very special edge cases that are valid 4 C but not valid C++.

One such example might 3 be the C declaration

int virtual;

which declares an integer 2 named "virtual". Since "virtual" is a keyword 1 in C++, this is not valid C++.

Score: 3

There is a large common core of C (especially 33 C89) and C++, but there are most certainly 32 areas of difference between C and C++. Obviously, C++ has 31 all the object-oriented features, plus the 30 generic programming, plus exceptions, plus 29 namespaces that C does not. However, there 28 are also features of C that are not in C++, such 27 as support for the (close to archaic) non-prototype 26 notation for declaring and defining functions. In 25 particular, the meaning of the following 24 function declaration is different in C and 23 C++:

extern void function();

In C++, that is a function that returns 22 no value and takes no parameters (and, therefore, is 21 called solely for its side-effects, whatever 20 they are). In C, that is a function which 19 returns no value but for which there is 18 no information about the argument list. C 17 still does not require a declaration in 16 scope before a function is called (in general; you 15 must have a declaration in scope if the 14 function takes a variable list of arguments, so 13 it is critical to #include <stdio.h> before using printf(), etc).

There 12 are also differences:


In C++, the answer 11 is 1; in C, the answer is normally 4 (32-bit 10 systems with 8-bit characters) or even 8 9 (64-bit systems with 64-bit int).

In general, you 8 can write code that will compile under both 7 C and C++ compilers without much difficulty 6 - the majority of my code does that all 5 the time. The exceptions are either a result 4 of carelessness on my part, or because I've 3 consciously exploited the good features 2 of C99 that are not in C++ 98, such as designated 1 initializers, or long long.

Score: 3

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

Score: 2

I personally would disagree with your professor.

Generally 16 speaking, C++ is based on C and in that 15 "sense" contains it and extends it.

However, since 14 traditionally people learned C and only 13 then the extensions of C++, your professor's 12 statement is incorrect since to use C++ correctly 11 you would need to master the C origins. It 10 is possible that when teaching you something, your 9 professor or textbook will not specifically 8 mention what came from which language.

In 7 addition, it is important to understand 6 that despite the similarities, not every 5 C program runs in the same way under C++. For 4 example, C structs are interpreted differently 3 (as classes with everything public) by the 2 C++ compiler.

When I teach, I teach the C 1 core first, and then go to C++.

Score: 0

If any of the students in the class intend 7 to become embedded software engineers, then 6 they may have no choice but to program in 5 C (see this question, and this one, among others).

Of course, having 4 learnt C++, it may be less of a transition 3 for them than starting from scratch - but 2 it still makes your professor's statement 1 untrue!

More Related questions