11

I have read in tutorials that C++ contains the entire C programming language.

However I have also read, in places like this that

If you learn C++ you will eventually learn most of C with some differences between the languages that you will learn over time.

So my question is only this:

If I know C++ very well, will I eventually learn the "real" C language (without any "differences") because the full C90 language is included in C++11?

Community
  • 1
  • 1
Startec
  • 12,496
  • 23
  • 93
  • 160
  • 3
    There are tons of little difference, but they're usually in the details. Examples that come to mind are type punning rules and welldefinedness of pointer arithmetic. Ultimately, C and C++ are very different languages. – Kerrek SB Jul 19 '15 at 20:06
  • 2
    Duplicate/related: [“C subset of C++” -> Where not ? examples?](http://stackoverflow.com/q/1201593/) – dyp Jul 19 '15 at 20:11
  • @Startec I am sure that "If I(you) know C++ very well" then you also knows C. Otherwise it may not be said that somebody lnows C++ very well.:) However i think that there are few programmers who know C++ very well. For example I know neither C++ nor C but this does not prevent me to answer many questions of others on C++ and C.:) – Vlad from Moscow Jul 19 '15 at 20:14
  • 1
    @dyp I'm conflicted; this is not an exact duplicate but looks like it's close enough to warrant being closed for being a duplicate. The key difference is that this question is not open-ended and has a definite answer. – fuz Jul 19 '15 at 20:15
  • 1
    @FUZxxl It answers the only on-topic question of the two OP posted, correctly with "No". – Baum mit Augen Jul 19 '15 at 20:18
  • 5
    *"I have read in tutorials that C++ contains the entire C programming language."* -- You need to find better tutorials. I'd also be interested in knowing where you got that misinformation. – Keith Thompson Jul 19 '15 at 20:24
  • 1
    I think all the answers so far missed an important aspect. Programming in C generally requires a different *mindset* with an imagined 1:1 mapping between source code and executable code. Programming in C++ means that you accept the compiler writing a lot of "invisible" code for you, e.g. destructor calls or "reference dereferencing". – Christian Hackl Jul 19 '15 at 20:25
  • Very good point, but I'm not sure the OP is yet at the stage of thinking about the machine code. ;-) – underscore_d Jul 19 '15 at 20:27
  • Tough question. On the one hand, **yes**, do C++ long enough, and you will eventually learn all of the C syntax and semantics (except for some very minor details which you don't even need for everyday C programming, and are easy to pickup). **However**, you will fail to learn how to **use** the features of C effectively, as you will have gotten used to solving problems the C++ way. Although you'll have a good understanding of C syntax and semantics, you will likely lack the experience with using ("only") them to solve real-world problems above beginner or intermediate level. – user4815162342 Jul 19 '15 at 20:34
  • Advanced use of C often includes implementing an application-specific run-time object system, possibly one that has nothing to do with the choices made by C++ - see the [`glib`](https://developer.gnome.org/glib/stable/) and [`gobject`](https://developer.gnome.org/gobject/stable/) libraries as examples. – user4815162342 Jul 19 '15 at 20:36
  • @KeithThompson on a lynda.com tutorial `"The C language itself is the basis of C++, and is entirely incorporated in its definition and forms the basic syntax of C++."` – Startec Jul 19 '15 at 20:37
  • fwiw, this is the 2nd question I've seen stemming from a poorly worded, overly generalised statement from that website in the past couple of days. – underscore_d Jul 19 '15 at 20:42
  • A link to the web page containing that quote would be helpful. – Keith Thompson Jul 19 '15 at 21:23
  • @KeithThompson, they are video tutorials (subscription based too), so unfortunately I can't supply that. These answers are sufficient though. – Startec Jul 19 '15 at 21:32
  • Shame that people are paying money to be given such misleading answers. Not surprising, though... – underscore_d Jul 19 '15 at 21:35
  • Overall I will say though, this bit of confusion aside it was quite a helpful tutorial, at least for a beginner. – Startec Jul 19 '15 at 21:40
  • 6
    C++ is *derived* from C, but is not a proper superset of C. Not every legal C program is also a legal C++ program, and those that are may not have identical semantics. A well-written C program does not look or behave much like a well-written C++ program (apart from very simple toy programs, anyway). So just learning C++ will not necessarily make you an expert in C by extension. – John Bode Jul 19 '15 at 23:37

4 Answers4

19

No, C++ is not a superset of the C language. While C++ contains a large part of C, there are subtle difference that can bite you badly where you least expect them. Here are some examples:

  • C has the concept of tentative definitions which doesn't exist in C++.
  • C does not require explicit conversion on assignment of void pointers to variables of concrete type.
  • C has different rules regarding const propagation.
  • C has something called the “implicit int rule,” which, although abolished with C99, appears some times and needs to be considered.
  • The C preprocessor has some features the C++ preprocessor does not have.
  • The C language has two styles of function definition, K&R-style and Stroustrup-style. C++ only has Stroustrup-style.
  • The lexing rules for C and C++ are different with neither being a subset of the other
  • C and C++ have different sets of reserved words. This can cause weird errors because an identifier is not allowed in the other language.
  • While C++ took almost all features from ANSI C (C89), many features were added to C in subsequent standard revisions that are not available in C++.
  • C++ has a different syntax, even for some parts that aren't new. For example, a ? b : c = d is a syntax error in C but parsed as a ? b : (c = d) in C++.
  • C guarantees that &*E is exactly identical to E, even if E is a null pointer. C++ has no such guarantee.
  • In C, a string literal initializing an array of characters can initialize an array that is at least as long as the string without the trailing \0 byte. (i.e. char foo[3] = "bar" is legal). In C++, the array has to be at least as long as the string including the trailing \0 byte.
  • In C, a character literal like 'A' has type int. In C++, it has type char.
  • C has a special rule to make type punning through unions to be legal. C++ lacks this language, making code such as

    union intfloat {
        int i;
        float f;
    } fi;
    
    fi.f = 1.0;
    printf("%d\n", fi.i);
    

    undefined behaviour.

fuz
  • 88,405
  • 25
  • 200
  • 352
  • 4
    One more: [C has "dynamic arrays", C++ doesn't](http://stackoverflow.com/questions/7458857/why-doesnt-c-support-dynamic-arrays-on-the-stack). – rwols Jul 19 '15 at 20:13
  • C++ provides plenty of, often better, ways to have resizable containers. Even if we limit ourselves to your single, artifically narrow definition of "dynamic array", C++ is probably about to gain this permutation, if you look at the draft of the next standard. – underscore_d Jul 19 '15 at 20:15
  • 2
    @underscore_d Of course C++ has better dynamic containers, I'm not saying that :-) – rwols Jul 19 '15 at 20:16
  • 2
    @rwols: C also has compound statements, named initializers, `restrict`, and type-generic macros. – Kerrek SB Jul 19 '15 at 20:16
  • 1
    Also `'a'` is a `char` in C++ and an `int` in C etc. etc. There is a reason the Dupe is closed as Too Broad. – Baum mit Augen Jul 19 '15 at 20:20
  • "_C has different rules regarding const propagation_" what is the difference? – curiousguy Aug 22 '15 at 06:46
  • 2
    @curiousguy In C++, `char**` can be assigned to `const char *const *` but in C that's not possible (cf. ISO 9899:2011§6.5.16.1¶1) because C++ has a more sophisticated rule for legal assignments of `const` pointers. – fuz Aug 22 '15 at 09:01
  • 1
    @underscore_d While there is a proposal to add *some* VLA functionality to C++17, that functionality only covers the most rudimentary cases. It does not allow you to use full multidimensional VLA types like C99 does. For instance, `int (*array)[height][width] = malloc(sizeof(*array}*depth);` (where `height`, `width`, and `depth` are all runtime values!) won't have a valid counterpart in C++17. – cmaster - reinstate monica Dec 22 '15 at 23:21
  • @paulsm4 Though this is highly subjective, I can only agree. – fuz Jun 20 '16 at 07:16
5

If I know C++ very well, will I eventually learn the "real" C language (without any "differences")

If you learn C++ properly, you will probably not need to use many of the standard techniques used in C. Theoretically you could program almost anything C in C++, with exceptions that have already been introduced. However, in reality, you wouldn't - or shouldn't. This is because C++ is a different language that provides a very different set of tools when used optimally.

Aside from the very basic elements like general syntax and fundamental types, these are two separately evolving languages, and they should be approached (learned, programmed) as such.

underscore_d
  • 6,309
  • 3
  • 38
  • 64
5

In broad terms, the C++ language is essentially C with a whole bunch of object oriented stuff added. Nearly all the code you could write in C will also compile and run just fine in C++.

However, there are a few corners of the languages where there are differences. The number of these have been slowly growing over time, but the languages aren't changing rapidly enough for that to be a significant problem.

If you only learn C++, then yes, you will eventually learn almost all aspects of the C language too. If you become expert in C++, then you will be able to identify and understand the places where small differences between the similar parts of C and C++ exist.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • Would the few aspects of the C language that would not be learned (paragraph three of your answer) be significant to cause problems if I had to common / basic C program or understand an existing one? – Startec Jul 19 '15 at 20:16
  • C++ provides different ways to do things. If you then go 'back' to C, you will be ill-prepared to use _its different ways_. There is not an automatic equivalence between learning one and learning the other. Many of us who have learned (and, as always in life, are still learning) both will confirm this to you. – underscore_d Jul 19 '15 at 20:17
  • 1
    @Startec: No, probably not. But if you *only* learn C++, then when you write your first C program you're going to expect a lot of stuff that just doesn't exist in C. – Greg Hewgill Jul 19 '15 at 20:17
3

I am not sure what "differences" might exist...

For example like this one:

In C:
void foo() means "a function foo taking an unspecified number of arguments of unspecified type"
[...]
In C++:
void foo() means "a function foo taking no arguments"

Community
  • 1
  • 1
nsilent22
  • 2,763
  • 10
  • 14
  • 1
    Good example, but one example does not really merit its own answer. – underscore_d Jul 19 '15 at 20:11
  • 3
    Answers on Stack Overflow should be mostly self-contained. An answer which is just a link to another resource is not as useful. – Dietrich Epp Jul 19 '15 at 20:12
  • Well, probably both of you are right, I shall add a comment instead, but my reputation level doesn't allow me to do it. – nsilent22 Jul 19 '15 at 20:20
  • @nsilent22 It's enough to just cite the relevant parts of the question (and its answer) you linked to. – fuz Jul 19 '15 at 20:24
  • 2
    @nsilent22 The point of this rule is that the linked resource might vanish. We don't want to have answers that are now useless because the resource linked to them is no longer available. – fuz Jul 19 '15 at 20:24
  • 1
    @nsilent22 You can click “edit” to improve your answer if you want. – fuz Jul 19 '15 at 20:26
  • @FUZxxl: Thank you for reminding me that. – nsilent22 Jul 19 '15 at 20:32