LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-07-2009, 01:18 PM   #1
SlowCoder
Senior Member
 
Registered: Oct 2004
Location: Southeast, U.S.A.
Distribution: Debian based
Posts: 1,250

Rep: Reputation: 164Reputation: 164
C vs. C++


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.
Old 12-07-2009, 01:52 PM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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

Last edited by ta0kira; 12-07-2009 at 01:54 PM.
 
1 members found this post helpful.
Old 12-07-2009, 02:14 PM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by SlowCoder View Post
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 View Post
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.

Last edited by johnsfine; 12-07-2009 at 02:22 PM.
 
Old 12-07-2009, 02:42 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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

Last edited by ta0kira; 12-07-2009 at 02:43 PM.
 
Old 12-07-2009, 07:34 PM   #5
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
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*)
 
Old 12-07-2009, 08:56 PM   #6
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Quote:
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*.
 
Old 12-07-2009, 10:28 PM   #7
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
I'm quite a fan of Boost in general, it turns C++ from horrible to absolutely *awesome*.
Using library when you do normal programming lazy programmer and cause bloat. LOL.
 
0 members found this post helpful.
Old 12-07-2009, 10:59 PM   #8
SaintDanBert
Senior Member
 
Registered: Jan 2009
Location: "North Shore" Louisiana USA
Distribution: Mint-20.1 with Cinnamon
Posts: 1,771
Blog Entries: 3

Rep: Reputation: 108Reputation: 108
C++ as a "better C" compiler

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.

~~~ 0;-Dan
 
Old 12-07-2009, 11:35 PM   #9
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Quote:
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.
 
2 members found this post helpful.
Old 12-07-2009, 11:43 PM   #10
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
you've not said even one thing on this Programming subforum that wasn't factually incorrect in some way
Actually i acknowledge it. You still should not be using libraries like that.
 
0 members found this post helpful.
Old 12-08-2009, 04:07 AM   #11
Gutsy_Iain_08
LQ Newbie
 
Registered: Jan 2008
Posts: 25

Rep: Reputation: 16
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.
 
Old 12-08-2009, 05:55 PM   #12
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
C is the father of C++ luke.

learn C first then C++.
unix system programming is in C.
 
Old 12-08-2009, 10:34 PM   #13
sparker
Member
 
Registered: Aug 2007
Location: Canada
Distribution: OpenBSD 4.6, Debian Lenny
Posts: 64

Rep: Reputation: 16
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.
 
Old 12-09-2009, 12:31 PM   #14
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by bigearsbilly View Post
C is the father of C++ luke.

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'll say if C++ is the goal, learn C++.
 
Old 12-09-2009, 03:17 PM   #15
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Gutsy_Iain_08 View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration