ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm going to start programming in Linux.
Should I start programming with C or C++ ?
Which one is better to start learning ?
And where can I get best practice to learn myself
It all comes down to personal preference, and people's opinions will surely set off a religious war when they say that C++ is better, or C is better. So I'll start off with my opinion, and you can all flame me for it...
I would probably suggest using the G++ compiler (which compiles C++ code) and then start learning C, with the intention of expanding your knowledge to C++.
By using a C++ compiler (eg: G++), you have extra features which make life a little easier, and you can bend some of the rules in C a little which makes it a bit easier to learn things, instead of being beaten over the head by the C compiler whenever you make a minor mistake.
As for the languages, C++ gives you more features, like classes, templates, etc ... however those features come at a cost ... classes require a little extra overhead for the CPU (they run just a tiny bit slower than non-class code), and templates make your code next to unreadable (yes, I know people who have lost their jobs for excessive use of templates). Most of the features you gain in classes can be incorporated into structures, which are available in C, and have a lower overhead.
Now the catch is that structures are easier to work with in C++ than they are in C.
So there you have it, my twisted reasons for writing "not-really C, but not quite C++" code using a C++ compiler.
I am a bit dogmatic. There are different classes of programing languages: procedural and others.
My point is that C++ defines its own class - nowadays C++ it is not an extension of C. There are
no 'procedural programing' in C++. The C and C++ are two different paradigms of programing. You cannot switch easily from one to the other. You will need to change completely your mind,
your way to think about programing tasks. Of course if you want to program efficiently. C and C++
programmers speak two different languages - that's they cannot understand each other.
C++ is a better language. The right subset of C++ is also a better beginner language than C.
C++ streams are better for beginner output that printf and much better for beginner input than scanf.
C++ strings are much better for beginner operations on text than C strings. C strings will make much more sense if you learn them after you have basic competence with pointers and arrays (features in common in C and C++ that you need to learn either way). Understanding C strings is important even for a C++ programmer (unlike printf/scanf), but you can get to them a little later in learning C++ than you would in learning C.
Quote:
Originally Posted by fcdev
So I'll start off with my opinion, and you can all flame me for it.
If you insist.
Quote:
classes require a little extra overhead for the CPU (they run just a tiny bit slower than non-class code)
That isn't an opinion. It is a falsehood.
C++ code can include hidden work, extra work that must be done at run time because of some design/code choice made by the programmer, but which wouldn't be obvious to a beginner programmer. The work implied by a chunk of C code tends to be more obvious.
So a beginner might easily write slower code in C++ than that same beginner might write in C. An expert will tend not to write slower code in C than C++ and when performance matters, an expert will know how to get maximum performance from C++ code, often more easily than getting the same performance from C code.
But there is no fundamental overhead to class code vs. non class code. There is no uniform slow down. The only slow down is when you code more work for your program to do than you intended it to do.
Quote:
templates make your code next to unreadable
That one is an opinion (see the difference?). But it is still wrong. Templates, like almost any feature of a powerful language can be abused to make unreadable or otherwise bad code. Templates can also be used to make much more readable and maintainable code in C++ than you can make in any other popular language.
Quote:
(yes, I know people who have lost their jobs for excessive use of templates).
I know a couple who should have lost their jobs for excessive use of templates, plus one more who was even worse in excessive use of templates, but too valuable in other ways to deserve losing his job for that. (One fired, two quit, and I need to maintain the messes they left, but none were fired or quit over templates). I can think of several other programming styles in C or C++ that are equally bad and few programmers who exhibit them. The fact that a few programmers use templates to write bad code doesn't mean templates are bad.
Hm, I am not surprised. Any language can be automatically translated into
assembler.
No. There are, for example, interpreted languages which are not translated into "C" or assembly. The point of "interpreted" in this case is that they are languages with late binding. I.e. the runtime engine not only is used for running the programs, but also, when necessary, for compiling and linking during runtime additional pieces of code.
But back to our C/C++ debate. It is very useful for the end user to understand when (compile or run time) and what price is paid for object orientedness. As johnsfine has already pointed this out.
For the OP it might interesting to learn both "C" and C++ at the same time, looking into implementation details.
The C and C++ are two different paradigms of programing. You cannot switch easily from one to the other. You will need to change completely your mind, your way to think about programing tasks. Of course if you want to program efficiently. C and C++
programmers speak two different languages - that's they cannot understand each other.
Ah, then I must be breaking the laws of this universe, being able to "easily" switch from one to the other. You can write programs that can be compiled and understood to be C and C++. The way of thinking applies to more than just C/C++; you can program in different ways using plain C as well. In some cases you can choose freely between them, using your own preferences, in some cases it's perhaps easier to choose one over the other. But claiming that C and C++ are from completely different worlds, "unable to understand each other" (whatever you mean by that, exactly, is a bit unclear), that's just not right.
Do learn both of them, at least the basics, and you'll see what they have in common and what they don't. The more languages (of different kind) you learn, the more you know, as goes with everything else. Don't limit yourself to one language, however, because that'll just limit what you can do.
You can learn 'basics' for C but what do you mean by 'basics' for C++? For me these are templates, classes, overloading, iterators, containers, streams, exceptions, class hierarchy, STL - all features which make this language so powerful. On the other, C side we have data types, arrays,pointers, structures, functions, all statements.
Now what is this common point for both of them?
what do you mean by 'basics' for C++? For me these are templates, classes, overloading, iterators, containers, streams, exceptions, class hierarchy, STL - all features which make this language so powerful.
That might be more than the average C++ programmer ever learns. It is absurd to call that the basics.
Quote:
On the other, C side we have data types, arrays,pointers, structures, functions, all statements.
Now what is this common point for both of them?
That was all part of the common points for both of them (data types, arrays, pointers, structures, functions, the basic statement types). The more significant question is what parts of the basics aren't in common. All the semantics of C and very close too all the syntax is part of C++. So all the basics of C are part of C++, but some of them aren't part of the basics of C++.
As I said before, printf and scanf are part of the basics of C, but in C++ you would use std::cout and std::cin. The common C string functions (strcmp, strcat, etc.) are part of the basics of C, but in C++ you would use std::string.
In both C and C++, the basics should include the major storage classes: Static (including global), Automatic, and Dynamic. But in C you would use malloc/free for dynamic, while in C++ you would use new/delete. Either way, you need to introduce the use of pointers for dynamic storage and elsewhere (hopefully later) deal with pointers to non dynamic storage and pointers into aggregates.
Learning templates is fairly advanced C++. Learning advanced features of templates that would be needed to write replacements for some of the std templates is even more advanced. In other threads people have pointed out that a beginning C++ is immediately using templates because of std::cout. I think that is like saying a beginning driver is using the car's fuel injection system as soon as he presses the gas pedal. It is true, but pointless. A beginning driver doesn't need to know the fuel injection system even exists. He just learns to use the gas pedal. A beginning C++ programmer can learn to use std::cout without learning about templates.
I think object oriented programming is a source of confusion to beginners. Object oriented fanatics and their influence over company programming standards and policies are a major impediment to good software engineering. Object oriented programming should be a tool a good programmer learns and understands and then uses when appropriate to the task. It is not a universal tool. If I were teaching C++, I would get through quite a lot of the basics (including structures) before introducing any true object oriented programming.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.