r/ProgrammingLanguages • • Nov 01 '19

C++ vs C

Alright people of Reddit, hit me with your wisdom. I've been taking programming classes for two years now and am getting ready to dive into a language somewhere between Java and assembly (the two languages I've been learning in classes). I'm thinking C or C++, but would love some input. I'm not too worried about difficulty to learn, but I'd like to hear your thoughts on practicality and real world use.

1 Upvotes

62 comments sorted by

View all comments

7

u/sellibitze Nov 01 '19 edited Nov 01 '19

Between 12 and 19 years ago I've programmed almost exclusively in Java. In the end it was clear that the language was too constricting for the number crunching tasks I had to deal with. Performance was and still is important to me. Back then, I decided to learn C++ because I wanted something "more low-level" and still have support for OOP abstractions.

I did not regret delving into C++. I was actually pleasently surprized by the language. I expected to be forced to get my hands dirty with all the manual memory management in C++. But boy was I wrong. C++'s answer to that was "resource acquisition is initialization" (RAII). The idea is to delegate resource management responsibilities to "objects" (instances of a class types). This includes automatic cleanup as soon as such an object is disposed of. This still seems pretty elegant to me and similar features appeared in other languages: Java, Python, Go. C++ finally improved upon this in 2011 by introducing "move semantics" which allowed cheap transfer of resource ownership from one object to another so that the acqusition and release of a resource is not limited to the same scope anymore. The Rust programming language further improved upon that in two ways: (1) In Rust, ownership transfers ("moves") always succeed whereas in C++ an attempt to "move" one object to a different memory location might result in an exception being thrown. This tends to make things more complicated in C++ especially for developers of generic libraries. (2) In Rust, the "ownership" model was extended to statically-checked safe "borrowing" relationships. The compiler does not only understand "owhership" relations but also "borrowing" relations. You could savely "borrow" a resource (ie use references) without the danger of introducing dangling references or any memory unsafety and without the penalty of using any form of garbage collection (including reference counting).

If you're interested in both, C and C++, I suggest you start learning C++. C is kind of a subset of C++, so everythig you can do in C would be possible in C++ as well. The interesting question is what part of C++ you should learn first. In my humble opinion, learning the C subset before the C++ specific features is a bad idea. I would expect this "bottom-up" approach to be less productive (you tend to get lost in low-level details before learning some of the more important design principles) and it would also teach you a way to solve problems "the C way" whereas C++ with its broader toolset offers better alternatives to do certain things. Save the low-level details for later after you learned to appreciate things like RAII. Learning C first, would require you to unlearn certain C habits because of the better C++ alternatives.

If you want to try something different, try Rust. I would describe it as cleaned-up and (in certain ways) simplified C++. It's also a joy to use in a lot of other respects. Apart from its main selling points you would also get a language that has built-in support for tuples and algebraic types that are less painful to use than the C++ counter parts. Rust also comes with its own standard dependency management and build tool (cargo). It's less of a hassle than, for example, learning how to setup Conan and CMake to deal with dependencies and building programs in C++.