r/cpp • • Jun 08 '20

Programming in C vs C++

I’ve read about the differences between them on the Internet but I wanna hear more from a more personal perspective. Which one would you recommend starting with and to be proficient with?

0 Upvotes

43 comments sorted by

View all comments

8

u/jselbie Jun 08 '20 edited Jun 08 '20

While I agree with some points brought up by u/jmuffat (especially the unlearning part), I hold a minority opinion here.

I've been coding in C++ for over 20 years. I came up through the Basic -> Pascal -> C -> C++ path as well. And while I think C++ is far better for production code, C is a better language to learn from - especially for those new to programming. But you should quickly pivot to C++ as soon you get the foundations of C down.

It used to be that good C++ text books would just start off teaching C fundamentals, and then dive into C++ classes by the middle of the book. Modern C++ books jump head first with too many complex code examples that can confuse new programmers.

With C programming, you learn all the foundations first in addition to programming fundamentals. This includes the following:

  • Basic types: int, char, double, arrays, etc...

  • string fundamentals: char*, strlen, strcpy,etc...

  • Structure types: i.e. struct - the foundation of what a class is in C++)

  • Functions

  • dynamic allocation: malloc and free

  • Preprocessor

  • Stages for building programs: preprocessor, compiler, linker.

  • Pointers: And the relationship pointers have with arrays, this the core of C and C++, even if C++ is trying to abstract pointers away.

In learning C, you'll be quickly concluding that foundations that you've heard about in other languages are missing or weak. That's when it's time to move to C++. Everything you learned above is still 100% applicable in C++. With your knowledge of C, you can dive right into classes, inheritance, virtual methods, template and the standard C++ library.

1

u/Wouter-van-Ooijen Jun 10 '20

I disagree, but maybe not the way you'd expect.

If your end goal is to learn C++ (otherwise, why bother), then start with C++. But NOT with the whole language.

A few years ago we changed our first serious programming course from C to C++, without changing the essence of what we learned the students. a[] became std::array, print became cout<<, pass-by-pointer became pass-by-reference, etc. That is (mostly) just syntax. What matters is to start with the fundamentals (loops, functions, some recursion). You can do that in any language, but if the end goal is C++ why start with a language like C that is sufficiently close to cause confusion? If it had to be a different language than C++, I might have preferred Python over C.