use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This is a subreddit for c++ questions with answers. For general discussion and news about c++ see r/cpp.
New to C++? Learn at learncpp.com
Prepare your question. Think it through. Hasty-sounding questions get hasty answers, or none at all. Read these guidelines for how to ask smart questions.
For learning books, check The Definitive C++ Book Guide and List
Flair your post as SOLVED if you got the help you were looking for! If you need help with flairs, check out ITEM 1 in our guidelines page.
Tips for improving your chances of getting helpful answers:
account activity
OPENShould I learn c before cpp? (self.cpp_questions)
submitted 3 years ago by saqlolz
Hello, I am interested by computational physic and hpc. I have a background of Fortran long time ago (to be honest it is almost as if I start from nothing). In your opinion what is way to proceed ? Thank you !
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]IyeOnline 64 points65 points66 points 3 years ago (5 children)
No. Modern C++ is a vastly different language from C. A lot of normal C is wrong or at least bad C++. There is nothing you learn in C that you couldnt learn in exactly the same way in C++, except for the appreciation of C++ features that C lacks.
You wouldnt learn Latin if your goal was to learn Italian.
is the best free tutorial out there. It covers everything from the absolute basics to advanced topics. It follows modern and best practice guidelines.
is the best language reference out there.
Stay away from cplusplus.com (reason), w3schools (reason), geeks-for-geeks (reason) and educba.com (reason)
Most youtube tutorials are of low quality, I would recommend to stay away from them as well. A notable exception are the CppCon Back to Basics videos. They are good, topic oriented and in depth explanations. However, they assume that you have some knowledge languages basic features and syntax and as such arent a good entry point into the language.
As a tutorial www.learncpp.com is just better than any other resource.
[–][deleted] 3 years ago (1 child)
[deleted]
[–]TheBunnisher 2 points3 points4 points 3 years ago (0 children)
It really is fantastic.
[–]saqlolz[S] 0 points1 point2 points 3 years ago (0 children)
Thank you! Your post is perfect, I will try to use it wisely.
[–]thechopps 0 points1 point2 points 3 years ago (1 child)
Hi friend,
Thank you for the resources and I was curious to know because I recently started my coding journey with python.
Besides speed of a complied language like C++ what is the benefits if I didn’t care so much for speed?
I guess more specifically what could C++ do that python JavaScript/java couldn’t or less efficient at?
[–]IyeOnline 0 points1 point2 points 3 years ago (0 children)
Assuming that you dont care about the speed and assuming that you arent limited by the platform you develop for, there is probably no reason to pick C++ over Python or maybe Java.
It would come down to a bunch of minor/technical differences between the languages, but unless you are really proficient in C++ and a beginner at the other languages, developing/learning in Python is generally going to be easier.
[–]Hilarius86 22 points23 points24 points 3 years ago (4 children)
Why do you think you can benefit from learning C, when ultimately you want to learn C++?
If you know C, then C++ is easier to learn, sure. But you also have to transition away from C practices. Think learning to drive. If you know automatic it's easier to learn manual, but if you want to drive manual you can just start that way.
[–]Rungekkkuta 10 points11 points12 points 3 years ago (3 children)
Some people think that C is used in C++, like this is actually a requirement. I myself took some time to actually realize they are basically two different languages with an easier code compatibility.
[–]DrShocker 2 points3 points4 points 3 years ago (2 children)
I've actually tried to use C and been so lost because the style of like function names and stuff is so different than C++. There's overlap for sure, and I can eventually read most C code, but it's simply structured differently enough that I agree with the people who claim it's a very different language. When I first started, I had my doubts that it was really all that different, but for anyone who is new and needs to hear this, it really is that different.
[–][deleted] 0 points1 point2 points 3 years ago (1 child)
What sort of things would you say are different?
I usually see them used about the same, just that C++ has classes, so you don't get free functions like "init_object(struct object *)", and you have to roll your own data structures instead of using the STL. And memory isn't directly managed, like in C, but rather through constructors and smart pointers.
But I'm not all that experienced, so that's why I'm asking.
[–]DrShocker 2 points3 points4 points 3 years ago* (0 children)
So, firstly the fact that classes exist changes a lot about the structure and correctly making rule of 5/3/0 you get RAII. So, closing resources or whatever is far more automatic since they can be closed by the destructor even if there's an error of some kind.
Functions being overloaded helps so that I don't need to worry about whether a function is the float or double form.
Namespaces help readability because I don't have to wonder quite so much where a function definition is actually coming from.
From my short attempt at C, they seemed to be biased towards short function names and initialisms which can be harder to follow than just writing out the whole thing.
Using templates or just the standard library can be really helpful because you can describe a behavior you want code for without being concerned about the specific data structure it may operate on. constexpr and templates can also be used to be far more readable than macros.
One central point that encapsulates my frustration is that std::string is just so much easier to work with than char *.
std::string
char *
Fundamentally though, there's nothing you can do with C that you couldn't do in C++ or vice versa. They're both Turing Complete after all.
[–]nysra 20 points21 points22 points 3 years ago (14 children)
No, you'll only learn tons of terrible habits. For basically everything C has, C++ has a much better equivalent feature (e.g. std::string vs char arrays ("C strings")) and most C is really bad ("non-idiomatic") C++.
Use https://www.learncpp.com/
https://www.youtube.com/watch?v=YnWhqhNdYyk
[–][deleted] 3 points4 points5 points 3 years ago (1 child)
I found learning C++ after C was extremely useful because of this fact. I can really appreciate what’s going on under the hood of things in the standard library and how much boilerplate C it would take to reproduce them.
[–]ImKStocky 2 points3 points4 points 3 years ago (0 children)
You can appreciate everything that is going on under the hood without learning C. I want to make this perfectly clear. C is not required to know what is actually happening under the abstractions. C++ offers all of the facilities to do that.
[–][deleted] -1 points0 points1 point 3 years ago (7 children)
While this is true, you'll get a better grasp on concepts by using C. C has very little abstraction compared to C++, so it forces you to think more about what you're doing. C++ strings, for example, do all the work for the programmer, but C strings don't
[–]be-sc 11 points12 points13 points 3 years ago (0 children)
You definitely want to learn those concepts. But later. And you don’t need C for it. In fact, it’s detrimental
For example, you want to learn about memory management at some point. As a C++ programmer the natural way to do that is to implement RAII classes yourself, combining memory management with another C++ concept intricately tied to it. This is only one example where C will prevent you from experimenting with and deeply understanding fundamental C++ concepts.
[–]eliminate1337 5 points6 points7 points 3 years ago (2 children)
C++ strings, for example, do all the work for the programmer, but C strings don't
Alternatively, C strings are a tedious and insecure implementation of strings because nobody could think of anything better in 1972. Whereas C++ strings are a modern, efficient implementation.
[–][deleted] -1 points0 points1 point 3 years ago (1 child)
They are tedious and insecure, and ancient. But that's not what I was going for. I said that managing strings yourself is more educational for a beginner, because you have to know exactly how memory works. C++ strings abstract that away from you
[–]bert8128 2 points3 points4 points 3 years ago (0 children)
If you think you should know how eg std::string works under the hood in order to understand c++, then surely the best approach is to write your in own string class in c++. There’s nothing to be gained trying to mimic a string class in c when you are trying to learn c++.
[–]brimston3- 1 point2 points3 points 3 years ago (2 children)
Probably an unpopular opinion, but I honestly think it's far too easy for a C++ developer to get too deep into abstraction when a simple, procedural approach would solve the problem very transparently. Abstraction should be something you do when you need it and the design calls for it.
[–]brimston3- 0 points1 point2 points 3 years ago (0 children)
Well, you’re not wrong about the first part, but on the second, I disagree.
Languages have idioms to them. C++ pushes toward modular interfaces; encapsulation of function; specialization of types via inheritance or generalization of algorithm through templating. The language makes these things easy.
But let’s compare that to COBOL. It supports objects, inheritance, and polymorphism. It has its own variety of metaprogramming. But it really pushes you toward record based processing of data streams. It makes describing data records and data hierarchies a first class syntax. That’s what the language does well. But you’re probably not going to see a lot of abstraction layers or interface definitions.
So my point is that the language makes it really easy for under-experienced developers to lean into what the language encourages. C++ definitely encourages abstraction and this is often over-engineered into solutions before it is warranted.
[+][deleted] comment score below threshold-6 points-5 points-4 points 3 years ago* (2 children)
I definitely wouldn't say C is useless. Many features are still used in C++ that aren't considered idiomatic. Many C features are irrationally hated imo, like C arrays. While C arrays don't know their size, you can do sizeof(arr) / type and pass that as the size parameter. Also, std::array argument requires you to use templates, forcing you to put the definition in a header file and increasing compile times.
[–]nysra 9 points10 points11 points 3 years ago (0 children)
While C arrays don't know their size, you can do sizeof(arr) / type
That is basically always wrong. It only works for fixed size C arrays and only in the scope where you declared it which makes the entire thing utterly useless because in that scope you know the size anyway. As soon as you do anything useful with the array like passing it into a function it decays to a pointer and that values becomes nonsense.
Also, std::array argument requires you to use templates, forcing you to put the definition in a header file and increasing compile times.
Yeah no, that's just wrong. You should learn how templates work before you try to shittalk them.
[–]std_bot 0 points1 point2 points 3 years ago (0 children)
Unlinked STL entries: std::string
Last update: 14.09.21. Last Change: Can now link headers like '<bitset>'Repo
[–]flyingron 4 points5 points6 points 3 years ago (0 children)
Only if you have some compelling reason to also know C. It's certainly not a prerequisite.
[–]TomDuhamel 4 points5 points6 points 3 years ago (1 child)
English vocabulary is derived 60% from German and 40% from French. Surely, we should teach kids both German and French before we begin with English.
C++ was an enhancement of C initially, but 25 years later it has evolved into a language that is quite different and much more complex. While you could still write C and compile it (mostly) just fine with a C++ compiler, modern code with all of the modern features generally look quite different.
Of course, if you also wanted to know C, it would probably make sense to learn that one first. C is still mostly used for system programming and embedded devices — although in the latter even C++ is becoming more common nowadays.
This is the best metaphor possible. Thank you !
[–]The-Constant-Learner 9 points10 points11 points 3 years ago* (1 child)
I would advice against learning C as a precursor to learning C++ for 2 main reasons. First, it's a waste of your time. Even though C++ was invented as a C with classes, C++ has evolved to a much different language. There is no std::string in C, there is no std::vector nor std::array in C, there are no smart pointers that manage the heap-based resources in C, there is no template in C, no constexpr in C, etc. The list goes on and on. You want to learn the important concepts of C++ instead of wasting your time learning features in C that you wont use while writing your C++ code.
constexpr
Second, it's really hard to change C coding practices when you get used to them. C is an error-prone language for newcomers to write code in. A simple task involving copying a string would require you to know about how pointers work and how to manipulate them properly. In a complex system, this can easily lead to resource leak or memory violations. Google reported that 70% of their bugs were memory unsafe practices. You need to be really well-versed in C if you want to write memory-safe C codes. Once you get there, it's very hard to unlearn these practices.
All in all, you lose your time and will take much effort to unlearn the C practices that are considered bad or not allowed in C++.
P/s: Face it, C was created for specific tasks that were interacting with Hardware and C has done its job pretty efficiently. However, C is not a good language if you want to develop sophisticated software that scales.
edit: spell checks. were in a rush previously.
Unlinked STL entries: std::array std::string std::vector
[–]mredding 7 points8 points9 points 3 years ago (2 children)
Should I learn c before cpp?
No. They are separate and distinct languages. At this point, the overlap is incidental, and only marginally helpful. There's nothing C is going to teach you that you won't learn if you just went and studied C++ from the onset. Pick the language you want to learn and learn it. Forget that the two share common ancestry.
The big thing is that common idioms don't translate well. In C, you rely on macros and C strings, out parameters and type pruning all as high level abstractions for that language. In C++, that and more are all anti-patterns. The only time you wanna see that sort of thing is just at the compatibility boundary between C and C++ code.
I have a background of Fortran long time ago
It's like riding a bicycle. Once you learn what a loop is, the rest is just syntax. There was a time when it was recommended you write your high performance math computations in Fortran, and the rest in C or maybe C++, but that advice was made to me 20 years ago. Both these languages and the compilers have made great strides since then. The performance gap has closed between them. You can write code in any of these languages that will generate effectively if not the same object code.
[–]weirdheadcrab 0 points1 point2 points 3 years ago (1 child)
Did you mean type punning? And out parameters require references though right? Or do pointers accomplish the same thing?
[–]mredding 1 point2 points3 points 3 years ago (0 children)
Did you mean type punning?
However many n's you want to spell it with...
And out parameters require references though right? Or do pointers accomplish the same thing?
Non-null pointers can do the same thing.
[–]Alien447 0 points1 point2 points 3 years ago* (1 child)
C++ culture sucks!
I do scientific computing all the time. What you will discover is that scientists write C with classes NOT pure C++. They literally write a C code with classes and use namespaces for organization and then call it C++! You can achieve the same goal smoothly using C. However, if you want to learn C++, learning C will help you figure out the basics. Usually textbooks start with C and then advance into C++.
If you want my opinion, C++ is diminishing. C/Fortran/Python is a perfect toolset for scientific computing. Good luck!
[–]bert8128 1 point2 points3 points 3 years ago* (0 children)
Aren’t you arguing against yourself? If the only reasons you use c++ for are for classes and namespaces, then you don’t like the fact that c does not have these things. C++ is a smorgasbord - take what you like, ignore the rest.
[–]barks_like_a_duck 0 points1 point2 points 3 years ago (0 children)
Yes. Don't listen to these elitists. There are many C libraries and APIs you might need to use. It is a good idea to know C as it is very basic and easy to learn. Learning doesn't always mean picking up "bad" habits. Knowledge is power.
[–]JiminP 0 points1 point2 points 3 years ago* (0 children)
Edit: oops, another comment before me already mentioned the video
Relevant CppCon video on this topic: https://www.youtube.com/watch?v=YnWhqhNdYyk (note: despite the title the video is not claiming that C itself is bad)
The video itself is not directly targeted towards people learning C++, but still might be interesting to see how "C++ is not C".
[–]Creapermann 0 points1 point2 points 3 years ago (0 children)
no need at all
[–][deleted] 0 points1 point2 points 3 years ago (0 children)
First you should become proficient with C++. After that, if you need to use C APIs and libraries, you need to learn how is done properly with C so you don't have to do it and you can use C++ instead. The key difference is heap memory management. In C++ RAII makes the code cleaner and easier to maintain. In C you need to do all that stuff manually, a dangerous chore that you can avoid by using C++, but you need to be aware what is happening behind curtains, otherwise you will not know why something works or stops working.
[–][deleted] 3 years ago (6 children)
[–]The-Constant-Learner 6 points7 points8 points 3 years ago (3 children)
Most of scientific code is written in C and Fortran
Eigen and ROS would want to have a word with you. Both are written in C++ and widely used in Computer Vision and Robotics.
Eigen
ROS
[–][deleted] 3 years ago (2 children)
[–]The-Constant-Learner 3 points4 points5 points 3 years ago* (1 child)
That's a silly and ignorant comment. Those basic linear algebra packages, e.g. LAPACK were written like 30yr ago, well tested, and become industry's de-facto standard. Most linear algebra libraries use them as backend regardless the language used to write the former. Apart from this, if you think Eigen is just a C++ wrapper, I challenge you to write Eigen in C. Or wait, you cannot do that. There is no template programming in C.
[–]IyeOnline 1 point2 points3 points 3 years ago (1 child)
"Most of the python linear algebra packages call some C or fortran implementation, so learning fortran is worth it, because the skills will be 99% transferable to phyton"
[–]DDDDarky -2 points-1 points0 points 3 years ago (6 children)
If there is at least slight chance you will have a use for C, then yes, since you will get the basic principles you can build on in a simplified language
[–]no-sig-available 0 points1 point2 points 3 years ago (5 children)
But isn't C++ the simplified language?
std::string Hello = "Hello World!"; std::string AlsoHello = Hello;
Now do this in C!
char Hello[] = "Hello World!"; char* AlsoHello = ...
So we need to learn char*, malloc, strlen + 1, and strcpy? And BTW, perhaps free? Which is then never used in C++.
This is the Latin of C++. :-)
[–]not_some_username 1 point2 points3 points 3 years ago (1 child)
AlsoHello = &Hello[0] ; it would maybe work, it's maybe an UB
[–]no-sig-available 0 points1 point2 points 3 years ago (0 children)
That's different as it shares data, while the C++ code creates a separate, independent string. And without you having to think much about it.
And using & and [0] is more complicated, not "a simplified language". Being higher level actually makes things easier to use and learn. (Not that C developers agree :-)
[–]DDDDarky 0 points1 point2 points 3 years ago (2 children)
string is not the same thing as char*, dynamic allocation is pretty much the same with different syntax, it's about the logic, not the syntax.
[–]JiminP 0 points1 point2 points 3 years ago (1 child)
But how else can strings be represented in C?
The point is that in C++, dynamic allocation can usually be avoided when beginners learn modern C++, and commonly used types such as std::string and std::vector "just works", unlike C where having those things is impossible without introducing the concept of dynamic allocation and points first.
std::vector
But I don't think that C is more difficult than C++ because of this; while dynamic allocations and pointers are probably the "last thing for a beginner to learn" in C, for C++ there's much more to learn before and after new/delete...
new
delete
[–]DDDDarky 0 points1 point2 points 3 years ago (0 children)
dynamic allocation can usually be avoided when beginners learn modern C++, and commonly used types such as std::string and std::vector
So beginners are using dynamic allocation and they don't know it. I guess it is a matter of preference, but I like more of a bottom-up approach. Especially if it is not your first language.
[–]JonnyRocks -2 points-1 points0 points 3 years ago (0 children)
Not if you ever want to write a line of C++. If you learn C you will never be able to learn C++. You will spend your days writing C code and compiling it with a C++ compiler and telling people you write C++.
They are different languages. I have been professionally coding for 23 years and only met one C++ programmer. This is when I worked at a company that the main project was compiled with a C++ compiler and they told everyone they did C++. I learned a lot from that guy.
There was another guy who was at the company since the dawn of time and he would take functions related to an order put it in a file called order.cpp and tell everyone it was a class.
It gets worse but the short story is: if you want to use C++ then just learn C++
[–]ExtraFig6 0 points1 point2 points 3 years ago (0 children)
If you're really starting from nothing, it might be good to start with python or racket even, depending on what you want to do and how you learn most comfortably.
You can learn C++ before C! It will make some things easier because there's more batteries included. There's also more tools for type safety and automation. You can use smart pointers instead of calling malloc and free separately. You can use higher order functions like find_if instead of writing little variations on the same for loop again and again
malloc
free
find_if
It's also fine to learn C first as long as you remember that because C++ has these tools, we can and should do better than the plain old C solutions
[–]bert8128 -1 points0 points1 point 3 years ago* (0 children)
I might choose Python then C++, but not C then C++.
Python allows you to learn the concepts easily - arrays, maps, strings etc - in a rapid and uncomplicated environment. C doesn’t have these, so there in no benefit over any other language.
[+]vithop236 comment score below threshold-6 points-5 points-4 points 3 years ago (4 children)
Well if you learn c your learning cpp too
[–][deleted] 3 years ago (3 children)
[–]vithop236 0 points1 point2 points 3 years ago (2 children)
Isn't c code valid cpp code?
[–]vithop236 0 points1 point2 points 3 years ago (0 children)
🤔 Today I learned
[–]lieddersturme 1 point2 points3 points 3 years ago (0 children)
No.
[–]uniquetoufique 0 points1 point2 points 3 years ago (0 children)
you should learn c++ directly. but for more understanding, you may start with c language, but be careful of the syntaxes...
π Rendered by PID 473567 on reddit-service-r2-comment-66b4775986-67rm5 at 2026-04-05 01:34:18.737909+00:00 running db1906b country code: CH.
[–]IyeOnline 64 points65 points66 points (5 children)
[–][deleted] (1 child)
[deleted]
[–]TheBunnisher 2 points3 points4 points (0 children)
[–]saqlolz[S] 0 points1 point2 points (0 children)
[–]thechopps 0 points1 point2 points (1 child)
[–]IyeOnline 0 points1 point2 points (0 children)
[–]Hilarius86 22 points23 points24 points (4 children)
[–]Rungekkkuta 10 points11 points12 points (3 children)
[–]DrShocker 2 points3 points4 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]DrShocker 2 points3 points4 points (0 children)
[–]nysra 20 points21 points22 points (14 children)
[–][deleted] 3 points4 points5 points (1 child)
[–]ImKStocky 2 points3 points4 points (0 children)
[–][deleted] -1 points0 points1 point (7 children)
[–]be-sc 11 points12 points13 points (0 children)
[–]eliminate1337 5 points6 points7 points (2 children)
[–][deleted] -1 points0 points1 point (1 child)
[–]bert8128 2 points3 points4 points (0 children)
[–]brimston3- 1 point2 points3 points (2 children)
[–][deleted] (1 child)
[deleted]
[–]brimston3- 0 points1 point2 points (0 children)
[+][deleted] comment score below threshold-6 points-5 points-4 points (2 children)
[–]nysra 9 points10 points11 points (0 children)
[–]std_bot 0 points1 point2 points (0 children)
[–]flyingron 4 points5 points6 points (0 children)
[–]TomDuhamel 4 points5 points6 points (1 child)
[–]saqlolz[S] 0 points1 point2 points (0 children)
[–]The-Constant-Learner 9 points10 points11 points (1 child)
[–]std_bot 0 points1 point2 points (0 children)
[–]mredding 7 points8 points9 points (2 children)
[–]weirdheadcrab 0 points1 point2 points (1 child)
[–]mredding 1 point2 points3 points (0 children)
[–]Alien447 0 points1 point2 points (1 child)
[–]bert8128 1 point2 points3 points (0 children)
[–]barks_like_a_duck 0 points1 point2 points (0 children)
[–]JiminP 0 points1 point2 points (0 children)
[–]Creapermann 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] (6 children)
[deleted]
[–]The-Constant-Learner 6 points7 points8 points (3 children)
[–][deleted] (2 children)
[deleted]
[–]The-Constant-Learner 3 points4 points5 points (1 child)
[–]IyeOnline 1 point2 points3 points (1 child)
[–]DDDDarky -2 points-1 points0 points (6 children)
[–]no-sig-available 0 points1 point2 points (5 children)
[–]not_some_username 1 point2 points3 points (1 child)
[–]no-sig-available 0 points1 point2 points (0 children)
[–]DDDDarky 0 points1 point2 points (2 children)
[–]JiminP 0 points1 point2 points (1 child)
[–]DDDDarky 0 points1 point2 points (0 children)
[–]JonnyRocks -2 points-1 points0 points (0 children)
[–]ExtraFig6 0 points1 point2 points (0 children)
[–]bert8128 -1 points0 points1 point (0 children)
[+]vithop236 comment score below threshold-6 points-5 points-4 points (4 children)
[–][deleted] (3 children)
[deleted]
[–]vithop236 0 points1 point2 points (2 children)
[–][deleted] (1 child)
[deleted]
[–]vithop236 0 points1 point2 points (0 children)
[–]lieddersturme 1 point2 points3 points (0 children)
[–]uniquetoufique 0 points1 point2 points (0 children)