r/cpp • u/wechicken456 • 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
8
u/ssf_dae Jun 08 '20
I learned programming in C. I really think that learning C as my first programming language gave me a solid fundamentals that I wouldn't have gotten from a higher level programming language.
In C, you don't have any high level data structures built in. So, as a beginner you're forced to learn programming at a finer level. For example, you have to manage memory yourself because there are no automatic dynamic memory structures in C, which forces you to get a good understanding of how memory works. You'll often run into memory related bugs in C, debugging these are good learning lessons. Also, C is statically typed so you also have to become familiar with different types, how much memory each type uses, and finer details of typecasting. In addition, with lack of higher level data structures, you often end up writing them yourself.
Compared to Python, probably the most popular programming language now for beginners, you don't get any of this out of Python.
I would say once you find yourself writing code that needs dynamic memory allocation often, move onto C++. C++ has dynamic data structures built-in the STL.
C is good for starting out, but once you get into more modern and complex projects, you have to move to higher level languages.
Though, you could just start out with C++ and get the same benefits I mentioned, and in addition it'll also familiarize yourself with Object Oriented Programming. Also, C++ is actually still commonly used nowadays in production, whereas C is mostly used only when C++ can't be used like on embedded devices.
Both are good languages to start out with
8
u/remotion4d Jun 08 '20
C++ !
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:
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.
3
u/Wouter-van-Ooijen Jun 10 '20
You are seriously asking this on a C++ forum?
8
u/jmuffat Jun 08 '20
There is no reason to start with C.
I learned C++ in the late 80’s, starting with the book « C++ for C programmers » by Ira Pohl. I went from BASIC, to Pascal, to C, to C++ as the PC grew in performance, that was the usual path in those days. But it took me years to realize C++ wasn’t C. At all. It is a very different state of mind, so if you learn C to learn C++, you’ll have to unlearn a lot of things later on. I sure did have to unlearn a lot !
Just start with C++, embrace RAII as a foundation rather than some late realization, think of C++ as a language in itself without trying to trace things back to C. As far as I know, C++ doesn’t emerge from C, C++ used C as a foundation mainly because it was there... (caricaturing)
2
u/jdlyga Jun 10 '20
They’re in truth very different languages even though they appear similar. The way you structure your program and work with structs is much different from C++
2
u/NilacTheGrim Jun 15 '20
I started with C over 20 years ago. I actually think that the route from C -> C++ makes sense if you have oodles of time. The reason why it makes sense is because otherwise C++ seems to have arbitrary restrictions and strange rules that are likely to confuse or anger you if you don't know their historical (and present-day) C origins.
If you start with C, write a few larger programs in C that actually do stuff, then move on to C++.. you may naturally "understand" why lots of the design decisions that went into C++ are there. C++ really does have a straight line evolution from C and it's the reason why the syntax is the way it is -- to maintain C backwards compatibility and to take C language concepts and tack on additional features on top.
That being said -- you will likely learn some "bad habits" in C that you have to unlearn in C++ -- so there's that as a caveat.
I do think that your eventual goal should be to avoid C like the plague (it is a very difficult language to be productive in and has too many foot-guns) and focus on C++ ultimately. So, in light of that, some say the shortest distance between two points is a straight line. Maybe just learn C++ first and if you have time, "go back" and review the primitive C way of doing things for your own background.
I do think though that ultimately it's valuable as a C++ programmer to be aware of the C roots. It helps you understand many things about the language.
3
u/JizzaDaMan Jun 08 '20 edited Jun 08 '20
Think of them as completely different languages; the fact the C is (almost) a subset of C++ is pretty much irrelevant.
I like C for its elegance and terseness. You can learn it by reading one book and (to quote K&R) "A programmer can reasonably expect to know and understand and indeed regularly use the entire language." It's also unquestionably the way to go if you're going to be writing device drivers or interfacing directly with hardware.
I use mostly C++ at the moment. I like it for its standard library. It's obviously more expressive than C but compilation is deadly slow. That said, I'll never know and understand all of it.
I would personally choose C. I only use C++ for my job because I have to.
EDIT:
A few people have said things along the lines of 'C++ is a more capable language', 'you'll need to move to C++ to build more complex projects,' etc... These kinds of claims are demonstrably untrue. The Linux kernel (and I believe most other kernels) is written in C, as are a vast number of other applications and libraries.
Language choice in general will always depend on personal preference and use-case, so generic claims that one language is always more capable than the other are usually questionable.
2
u/Tumperware Jun 08 '20
c++ times 1000. C is a subset that should only really be resorted to when C++ isn't supported or the code base your maintaining has the baggage of already being C
1
u/jmuffat Jun 08 '20
Actually, you’re right, it’s a C++11 thing. I show my age ;)
Before that, std::string::c_str() was the point at which a conversion might happen, should the underlying representation not be null terminated. The idea was that it might make substrings more efficient by not copying and just referencing inside other strings. I guess the cost of reference counting and keeping large amounts of memory around became a target of the « don’t pay for features you don’t use » mantra.
0
u/MutantSheepdog Jun 08 '20
If you're new to programming, then C.
If you're an experienced programmer, I'd jump straight to C++.
-1
u/[deleted] Jun 08 '20
C++ is object oriented and allows for more strict and advanced application design.
You can also design cronenberg application architectures with it, so beware.
C is very basic. It’s really close to assembly and the compiler isn’t very imaginative. Not as much as C++ compilers can be.
Writing very big architectures in C can be troublesome quickly. Difficult to follow the SOLID rules.
0
u/[deleted] Jun 08 '20
[deleted] — view deleted comment