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.
For those of you who have coded in both C and C++, can you provide reasons for using one over the other? I understand C++ is supposed to be more user-friendly, as well as have object-oriented capabilities that C doesn't have.
Your words would be much appreciated.
Click here to see the post LQ members have rated as the most helpful post in this thread.
We could throw generalizations out there all day, which will inevitably lead to a battle because the English language doesn't allow short, yet accurate communication of what one means. It's bound to happen, so I guess I'll get it started:
C:
straightforward dynamic symbol naming
lowest common denominator for glibc API
I/O is often "less ugly" than all of the OOP overhead in STL I/O
C++:
smart pointers and other automatic cleanup
destructors executed upon stack unwind
much higher level of abstraction possible
I rarely use C++ without C unless it's a template library. I try to use C without any C++ whenever possible because C++ complicates dynamic linking and some things are simpler if done in C directly.
Kevin Barry
For those of you who have coded in both C and C++, can you provide reasons for using one over the other?
Only reasons for using C++ rather than C. I wouldn't use C where there is any choice.
I know C++ well enough to use the subset of C++ appropriate to any task. Whatever you might want to do in C, the right subset of C++ is better.
Quote:
I understand C++ is supposed to be more user-friendly,
That is a strange view point. C++ is enormous and complex in ways that wouldn't normally be called "user-friendly". I think a well chosen subset of C++ would be easier to learn and easier to program in than C. But people rarely approach C++ that way. If you learn too much of it at a time, C++ can be quite hard to learn.
C has a number of minor mis features left over from original language design errors or from the days when it wasn't practical to do more work inside the compiler in order to require less work from the programmer.
C++ corrects several of those, making it "friendlier" in that direction. But many C compilers let you use a few extensions from C++ anyway, so you aren't stuck with all the flaws even in C. Also, C++ is less friendly in so many ways, the fact that it fixed a few flaws is trivial.
Quote:
as well as have object-oriented capabilities that C doesn't have.
C++ is a much more powerful language. A lot of that comes from OO. But a lot of it is only distantly related to OO.
Quote:
Originally Posted by ta0kira
straightforward dynamic symbol naming
lowest common denominator for glibc API
Name mangling is one of the worse features of C++. It lets you use a non object oriented linker to link object oriented compiled code. But that benefit isn't enough to balance the cost.
Because of that flaw in C++, a lot of C++ code should be written with extern "C" interface.
But even when the interface should be "C", I write both sides in C++ (a C++ caller coded as if it is calling a C function and C++ function coded as if it was to be called by a C program).
Quote:
I/O is often "less ugly" than all of the OOP overhead in STL I/O
Most of that overhead exists only a compile time. The C++ compiled code for I/O usually has less overhead than doing similar things in C. Few parts of either language are as ugly as scanf.
Processing text is a nightmare in C, so that's usually my breaking point. On the other hand, I can't stand using nonsensical chains of << and >> for C++ I/O, so I almost always use fprintf, fgets, and strsep directly even in C++. If you're going to use one, you need to stick with it because it's best not to mix buffers. The level of complexity rises above what one should do in C very quickly, as far as the things I use compiled languages for, but it's relaxing to be able to write small tools in nothing but C. I also try to use C APIs, almost always with C++ back ends.
Kevin Barry
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339
Rep:
C++:
PROS:
*Object orienented
*Still compatable with low level while keeping a nice abstraction
*Avoids the horrable scanf ()
*Operator overloading and other extendabiselitis
*Nice memory allocation
CONS:
*Mashing the **** out of the function names
*Harder to learn
*Encourages bugs
Personally i only use C++ when
A:i need classes/extendibility
-or-
B:it would otherwise require the use of the C scanf (*shutter*)
Processing text is a nightmare in C, so that's usually my breaking point. On the other hand, I can't stand using nonsensical chains of << and >> for C++ I/O, so I almost always use fprintf, fgets, and strsep directly even in C++.
Perhaps it's because I've done far too much of it for programming contests and other things, but I've gotten used to doing text processing in C++ quite easily. Anyhow, I use boost format to eliminate the ugly << stuff. It's possible to mix the C and the C++ versions safely, if you properly flush out every time.
I'm quite a fan of Boost in general, it turns C++ from horrible to absolutely *awesome*.
I've written plain-C forever. I've taught C and C++ at the university level. There is lots and lots of value to features of a C++ compiler even if you avoid most of the "object oriented" features.
Consider the standard-C (plain) library routines fopen( ), fread( ),
fwrite( ), and fclose( ). All of these hang off a common data structure.
Plain-C will happily let the code step all over the contents of that structure. Same code compiled with C++ lets you declare the data contents of the struct as "private" and you will get a compile-time error.
If you are familiar with plain-C, remember the fopen( ) model as you write your code. Start using "class" instead of "struct" as you get more comfortable with C++. You will be able to go a long way to much better code without biting off "object oriented." Eventually, you will want to embrace objects and their benefits, but let a C++ compiler become your friend and help you write better plain-C.
Using library when you do normal programming lazy programmer and cause bloat. LOL.
Please stop spewing nonsense before you cause real damage. From what I can remember, you've not said even one thing on this Programming subforum that wasn't factually incorrect in some way. Come back once you get real experience and some semblance of a clue.
I haven't really coded in C, but I understand that C++ has a heaps better design for data structures like <list>, <vector>, <map>, etc. In normal C you tend to use arrays.
It really depends on what the actual task you are trying to accomplish is. When you don't really need to program at a lower level then C++ is usually the better choice.
For example the majority of programming that I do is networked and C is more flexible and straightforward when using things such as raw sockets. But for maybe a nice GUI application I would maybe use C++ and Qt.
Then again it really does come down to personal preference. They both have their pros and cons and people should just use whatever they are most comfortable with. Doesn't hurt to know both though.
learn C first then C++.
unix system programming is in C.
I don't know, Billy ...
I found it (still find it) hard to get my head around
some of the OO stuff that C++ is all about, and I'll
blame that on the fact that I had been doing functional/
procedural coding to start with.
I haven't really coded in C, but I understand that C++ has a heaps better design for data structures like <list>, <vector>, <map>, etc. In normal C you tend to use arrays.
???
With good use of macros and/or better than CPP preprocessor one can use this stuff in "C" easily.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.