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 ?

6

u/CoffeeTableEspresso Jun 08 '20

I mean, you can't really use strings in C++ properly without understanding how char * works...

Things like:

  • why are string literals not std::strings?

  • why doesn't adding two string literals work?

  • interfacing with old/C libraries requires

  • etc

You need at least a basic understanding of these things before moving on to C++ strings. Your going to have to learn C strings at some point in C++ anyways..

When I TAed an intro C++ course, students very quickly ran into trouble with C++ strings because they didn't understand C strings.

2

u/jmuffat Jun 09 '20

And me who was all « C++ has gone a long way to be more accessible to newcomers » !!! You’re right, it’s a long road, I can see that.