Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ as a first language [closed]

Tags:

c++

People also ask

Is C still used in 2022?

Let's say you are new to programming. There are a variety of languages to choose from. Many people will recommend Python as your first language because of its short syntax which makes it very attractive.

Should C be your first language?

While C is one of the more difficult languages to learn, it's still an excellent first language pick up because almost all programming languages are implemented in it. This means that once you learn C, it'll be simple to learn more languages like C++ and C#.

What language is close to C?

These languages are C++ (also known as C with classes), C#, Python, Java, JavaScript, Perl, PHP, Verilog, D, Limbo and C shell of Unix etc. Every language uses C language in variable capacity.

Is C language still relevant 2021?

Despite the prevalence of higher-level languages, the C programming language continues to empower the world. There are plenty of reasons to believe that C programming will remain active for a long time. Here are some reasons that C is unbeatable, and almost mandatory, for certain applications.


I don't understand why people still confuse "language" with "library". (Referring to the linked answer.) So what if C++ doesn't have a "native" concept of audio? There are lots of libraries out there, which you can readily use with C++, and which are probably better suited to your specific needs than any "catch-all" "standard-library" audio processing API could be.

</rant>

C++ is a difficult language. There are others that are easier to learn. I would never argue about that.

But C++ is easily one of the most powerful languages around. It can be highly efficient, and highly elegant, at once. Of course, nothing keeps you from making a fine big mess of everything, either.

If I recommend C++ as a first programming language? Actually, I don't know any better. Others might protect you from making mistakes, and make initial success (e.g. your first GUI application) easier. But if you build on a foundation of C++, you will always be on a secure footing. You might never have to chose another language at all, actually. But if you want / have to, you will find it ridiculously easy.

An experienced C++ coder can do e.g. Java at full speed in a matter of weeks. The other way round? Much, much more difficult.


Many years later, I felt like I should amend this answer. Since my kids asked me to teach them programming, I started to do so. I found myself actually not starting with C++... because I showed them BF first. In absolutely no time at all, they understood about memory and pointers. Then I went on with C++, and we're enjoying ourselves immensely so far.


Yet some more years later, and I felt I really should add this excellent lecture by Kate Gregory from CppCon 2015 to this answer:

Stop Teaching C.


Let me put it this way:

I took and passed with a 94%, the Sun Java Certification without ever having compiled a line of Java (that was nine years ago). I was able to do this, because I knew C++ very well.

On my first professional job, I was hired as a tester. On my second day, I was told I could program instead of test, if I learned enough PowerBuilder to be useful -- in two weeks. Because I knew C++ so well, PowerBuilder was easy.

C++ is hard, but if you learn it well -- read Stroustrup's The C++ Programming Language, do the exercises too, read his Design and Evolution of C++, read the C++ Faq and the Meyers books and Herb Sutter's books, read comp.lang.c++.moderated, read Coplien's Advanced C++ Programming Styles and Idioms no matter what OO language you want to code in -- if you learn it well, every other OO language is easy.

Learning C++ is excellent preparation for learning to code. (And you'll become a fair C programmer too, but read K&R to get to be a good C programmer.)

Oh. And C++ is really fun to program in, much less verbose than Java, and much more flexible. You're really in charge, not the IDE or the language. You can shoot off your own foot, but you'll be in charge of doing it. And read Alexandrescu's book once you know templates, it'll amaze and dumbfound and delight you.

C++ is fun. Read the "Curiously Recurring Template Pattern": it looks like this

template< class  T> struct base {};

struct derived: base< derived > {};

Yeah, you got that right: it's a class that derives from a base that's templated on the deriving class. Now that sounds just obscure and silly, but there are many good uses for it, and just the other day I was beating my head because you can't do this in Java (because Java uses type erase, not templates, for generics), but if I could have used it, I could have saved lots and lots of code repeated in lots of classes.

There's so much more in C++, and if you limit yourself to Java or .Net, you'll never discover it.


C++ isn't an easy language to learn as noted by everyone else, but there are a few reasons that it makes a decent place to start:

  • it doesn't tie you to a specific programming paradigm (e.g., Java and OOP) so you can experiment with different techniques easily
  • C++ doesn't have the bells and whistles so you can concentrate on making the building blocks if you want to know how they are built
  • since it is a language and not a platform, you can choose the library or libraries that best fit the task at hand or your chosen programming style (e.g., choosing something like Poco for a Javaesque feel, ACE for the gritty early style OO, etc.)
  • it is a general purpose programming language, not a platform (had to say that twice)

The trick is learning it carefully and correctly the first time. I know that I, like many others, have struggled with it by learning it as "C + objects" before really understanding it. It is a hard language to learn. I would recommend looking for an experienced C++ mentor to attach yourself to as well as reading a lot of the "highly recommended" books on how to and how not to write C++. Avoid learning bad habits up front.

For a starting reading list in no particular order:

  • "Accelerated C++" by Koenig and Moo
  • "Effective C++/STL" by Scott Meyers
  • "Object-Oriented Software Construction" by Bertrand Meyer
  • "Expert C Programming" by Peter van der Linden
  • "STL Tutorial and Reference Guide" by Musser, Derge, and Saini
  • "The C++ Standard Library" by Josuttis

Lots of support for c++ here, but I have to disagree. Every language has it's plusses and minuses (some achieve a better balance than others, of course).

That being said, for a first language, I'm convinced most people will learn how to program more effectively with a language that a) doesn't have an edit-compile-run cycle (which is inherently slower for you as the programmer b) has garbage collection c) has good runtime introspection. Best of if there is an interactive evaluator as well, to mess about with.

Development (if not runtime) is almost invariably faster in such languages, there are fewer things to trip you up, and there are better tools to help figure what went wrong when it did.

Which isn't to say learning the vagaries of memory management in a language like c++ isn't something worth doing, etc., etc., it just isn't the place you really want to start.

Another issue with c++ is that for all its practicality the sheer size of it added to the fact it has incompatible parts that can lead to subtle issues best avoided as a beginner.

edited: John D brings up the point in a comment that some people who learn first on a GC'd language (which is by no means limited to "scripting" languages) never learn pointers later. This is a red herring, though. Some people fail to learn all sorts of things, for all sorts of reasons. I maintain that worrying about pointers is a distraction when you're learning the basics. At some point you may well need to learn about manual memory management, but it's not fundamentally important to do this at first. Or more accurately, the gains made by avoiding this are more important than the costs.

Let me be clear though, I'm not saying that c++ is an inferior choice for a first language because it lacks garbage collection. That's just one of many issues that get in the way of learning the fundamentals of programming well. The real problem is that it is a large complicated language, with quirks all over the place and even internal inconsistencies (e.g. pointers and exceptions don't play well together). Of course all of this can be managed properly and you can learn good practice to keep from getting in trouble.

However, none of this will make much sense to a real neophyte at first. So some of learning it will end up being "do it this way, not that way". "why?" "because I said so --- you won't understand the details yet". Hardly an auspicious start.

The bigges gains for learning a first language though are probably in avoiding a edit-compile-run cycle (of any kind) and having good introspection.