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.

7

u/jmuffat Jun 08 '20

I guess what you’re really saying is C++ might not be the right choice of first language to learn, and I could agree with that. But I cannot agree that it is a good thing to learn char*, strlen, strcpy etc only to find out they’re terrible and, then, go misunderstand std::string because you think of them in terms of char*. Same for malloc/free, we all have been in the hell of tracking leaks, it’s not fun and it only teaches you one thing : that you need some way for the machine to manage this for you.

You’re probably someone who likes to know how things work before going to an abstraction, and that works : knowing assembly language was a big plus for me when learning C, I could see why things were one given way, what problems they were trying to solve. Yet, I wouldn’t recommend learning asm before learning C, it’s quite an easier trip the other way round... Do you see what I mean ?

0

u/[deleted] Jun 08 '20

[deleted]

2

u/jmuffat Jun 08 '20

Oh definitely ! I've worked in games and that's a domain where you end up rolling your own everything at one point or another (shortcut all the things !!!) : strings, malloc/free, sorts, IO's, TCP-replacement over UDP, you name it... That's the beauty of C++ (and C), you can go that far. But would you want to go there as the first things you learn ?

If I had to train a beginner, I would start with C++, not C, and I would start with std::string not char*. We would build small utilities, that do simple things and complicate as we go. At some point, we'd reach places where we'd need performance and that usually begins with controlling how memory gets in and out of cache, which is all about laying things out in memory. At that point, it makes sense to go there. But at the beginning, when you're just parsing command line parameters and text files, why reinvent the wheel ?

Writing this, I think we're really always revolving around the more important question that is "why do you learn C++ ?", "when do you use C++ ?", and it generally boils down to the performance&low-level questions. Which implies "good understanding of computer architecture required"...