C vs C++

Mixolydian

Lifer
Nov 7, 2011
14,570
91
86
gilramirez.net
I know this is a very common discussion topic and often turns into a heated debate, but since you guys are very talented, I'd like to get some honest answers.

I don't know a whole lot about programming. I know there is C, and then there's C++, which is object-oriented (not exactly sure what that means still) while the former is not. Both are very popular (the most popular?) computer programming languages.

So in what situations would it be better to write in C++ as opposed to C, or vice-versa?
Is one "easier" or "more-efficient" than the other?
 

Cogman

Lifer
Sep 19, 2000
10,277
125
106
As far as performance goes, they are about even. The rest just comes down to features. C++ is definitely more full featured than C. It supports a lot.

The thing is, many don't like C++ because of its complexity. Because it has so many features, it is easy to write C++ that is hard to understand. To some, Cs more limited syntax and feature set ends up being a boon.

When one is better than the other comes down to preference mostly. The two are pretty interoperable.
 

Jaydip

Diamond Member
Mar 29, 2010
3,691
21
81
I know this is a very common discussion topic and often turns into a heated debate, but since you guys are very talented, I'd like to get some honest answers.

I don't know a whole lot about programming. I know there is C, and then there's C++, which is object-oriented (not exactly sure what that means still) while the former is not. Both are very popular (the most popular?) computer programming languages.

So in what situations would it be better to write in C++ as opposed to C, or vice-versa?
Is one "easier" or "more-efficient" than the other?

As cogman pointed out their performance is on par.C++ fixes some of issues with C and then introduces something of it's own.Due to backward compatibility any well written C code is valid C++ code as well.So there is really no reason not to adopt C++.
 

Cogman

Lifer
Sep 19, 2000
10,277
125
106
As cogman pointed out their performance is on par.C++ fixes some of issues with C and then introduces something of it's own.Due to backward compatibility any well written C code is valid C++ code as well.So there is really no reason not to adopt C++.

* C89 code. There are some C99 features not in C++
 

KenJackson

Junior Member
Jun 16, 2013
14
0
0
The thing is, many don't like C++ because of its complexity. Because it has so many features, it is easy to write C++ that is hard to understand.

I'm pleased to see someone else say this. For years I read and heard how superior C++ was. I knew that I didn't understand its concepts very well so I studied and learned.

But now that I've been working primarily in C++ for a few years, I've decided the supposed superiority is a crock. The hallmark of C++ seems to be "information hiding", but for every detail you hide in C++, there are two or three new complexities that have to be mastered.

Yes, it's true that manipulating pointers in C, especially with pointers to pointers, is difficult to understand at first. But now that I understand it, I find it all very straightforward. But following those C++ lists with their long template types, especially when debugging, is very taxing.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Just because templates exist doesn't mean you're forced to use them. I've been writing C and C++ code since last century and have yet to use them except indirectly through libraries.

From a maintenance POV, I'd say they rarely make sense outside of library code, just like operator overloading which we discussed recently.

I'd also say using C++ will usually make more sense than C. Just ignore the "fancy" features like templates and operator overloading in your own code unless you have a very good reason to use them.
 

Pia

Golden Member
Feb 28, 2008
1,563
0
0
Neither of these languages is general purpose. Both are quite tricky to use, and highly unreliable if you don't know what you are doing. You shouldn't choose to use either language unless you have a specific reason to. Usually that reason is performance. In particular: both languages are bad for a first programming language, for slightly different reasons.
But now that I've been working primarily in C++ for a few years, I've decided the supposed superiority is a crock. The hallmark of C++ seems to be "information hiding", but for every detail you hide in C++, there are two or three new complexities that have to be mastered.
Dunno where you got the idea that C++'s hallmark is "information hiding". Hiding what information and from whom?

C++ is basically a big grab bag of tools designed to allow you to be more productive and to write safer, cleaner, more readable code over comparable C code. Where its design differs from general-purpose languages like C# and Java is that it never takes any control or performance away from you if you don't want to. This is why it's basically never worse than C and should be preferred when special circumstances do not dictate otherwise.

C++ takes some more time to learn than C because it's a bigger language, but that difference is partly illusory. For instance, when you want to write any real code, you need various common algorithms, data structures, string handling and other facilities. C++ standard library provides a decent amount of those. C standard library provides nothing worth using. So even if you "knew C", you'd then immediately be faced with the problem of finding and learning decent libraries for those basic facilities, and whatever you ended up with would be non-standard.
 

Train

Lifer
Jun 22, 2000
13,861
68
91
www.bing.com
Wanna see some C/C++ rage?

Go read the discussion thread where Linus Torvalds explains why the Linux kernel is written in C and not C++.

Within the sea of douchyness that is Torvalds, there are a handful of intelligent points he makes.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
But even if his points are valid (and I'm too lazy to go dig for them), application and OS kernel development are apples and oranges.

For application development, time to complete the work and ability to maintain the code are usually the most important points, and well-written C++ has advantages for both over C.
 

OneOfTheseDays

Diamond Member
Jan 15, 2000
7,052
0
0
But even if his points are valid (and I'm too lazy to go dig for them), application and OS kernel development are apples and oranges.

For application development, time to complete the work and ability to maintain the code are usually the most important points, and well-written C++ has advantages for both over C.

Agreed, he's arguing apples to oranges here. For low level OS kernel work, there really isn't much of a need for C++ in favor of straight C. I certainly would not recommend it.

C++ really shines with middleware components where you need portability, performance, and maintainability. An experienced developer can achieve all of these goals with C++.
 

Cogman

Lifer
Sep 19, 2000
10,277
125
106
I'm pleased to see someone else say this. For years I read and heard how superior C++ was. I knew that I didn't understand its concepts very well so I studied and learned.

But now that I've been working primarily in C++ for a few years, I've decided the supposed superiority is a crock. The hallmark of C++ seems to be "information hiding", but for every detail you hide in C++, there are two or three new complexities that have to be mastered.

Yes, it's true that manipulating pointers in C, especially with pointers to pointers, is difficult to understand at first. But now that I understand it, I find it all very straightforward. But following those C++ lists with their long template types, especially when debugging, is very taxing.

Honestly, I was just trying to present the case against C++ as best I've seen it presented. I don't really agree that language complexity is a good reason to state that one language is worse than another, but I can understand how others can come to that conclusion.

For me, it is all about realizing that shitty code can be written in any language, so instead of worrying about the WTFs of a language, you should worry about the feature set that it offers.

Take C# vs Java as an example. In a lot of ways, C# is a more complex language than java. In fact, it is almost a superset of Java. Yet, when asked "Which is better, C# or Java" (and ignoring anti-ms/portability arguments for now) C# almost always gets the "Yes, it is a nicer language" acknowledgement. Yet for some reason, C++ doesn't get the same sort of consideration.

If I had my choice, it would be C++ over C. Objects are nice. Destructors are nice. Operator overloading is nice. Namespacing is nice. Templates are nice. The WTFs come in when these systems are abused. However, is that any worse than someone embedding a whole bunch of crazy logic in a C macro? I don't think so. I don't think that "People will do bad things with this feature" is a good reason to exclude a feature. *cough*java*cough*

But even if his points are valid (and I'm too lazy to go dig for them), application and OS kernel development are apples and oranges.

For application development, time to complete the work and ability to maintain the code are usually the most important points, and well-written C++ has advantages for both over C.

Meh, his points went more like "C++ is bad because all the C++ developers I know are bad"

C++ is a horrible language. It's made more horrible by the fact that a lot
of substandard programmers use it, to the point where it's much much
easier to generate total and utter crap with it. Quite frankly, even if
the choice of C were to do *nothing* but keep the C++ programmers out,
that in itself would be a huge reason to use C.

He goes on to bemoan how inefficient C++ is (yeah...) and also how unportable the STL and BOOST are (yeah...).

Honestly, there isn't a real reason not to use C++ for kernel development, other than the fact that linus hates the language.
 

dagamer34

Platinum Member
Aug 15, 2005
2,591
0
71
C++ has tons of features, tons of tools, but if misused, it will kill you. It's a double-edged sword.

The average "good" programmer will know how to read both. Good solid design principles will keep you and future people who read your code sane.

I never really did get the "What should my first language be?" question, because any good programmer will need to know several. It's not the actual language you need to learn, it's the way languages work that are important.

You aren't going to master a language by memorizing a dictionary. ;)
 

soulbadguy

Member
Jul 1, 2013
30
0
61
Without going into the difference between the two, If you are looking for a "first language", i would say neither is particular suitable. It might be argued that C being a simpler (as in C has less concepts than C++) might be a better bet. But in my perspective, the "language" itself has very little impact on the learning experience. Most important is the learning environment, the IDE, the debugger, the tutorials etc... . C and C++ are older languages from before the "democratization" of programming and expect more from the developper then say C# or java
 

KenJackson

Junior Member
Jun 16, 2013
14
0
0
Dunno where you got the idea that C++'s hallmark is "information hiding". Hiding what information and from whom?

Information hiding is very much a part of object oriented and therefore C++. But I exaggerated by calling it the hallmark. I should have called it "the abused feature that irritates me most."

Remember back in the 80's when people were excitedly explaining why we need OO? The example I heard most was printing a circle. You don't have to know how to print a circle because the circle object knows how to print itself. It just means that instead of a straight forward piece of procedural code, the code gets split up and scattered all around in a bunch of tiny little illogical functions that you have to go find when you're debugging.
 

Train

Lifer
Jun 22, 2000
13,861
68
91
www.bing.com
Information hiding is very much a part of object oriented and therefore C++. But I exaggerated by calling it the hallmark. I should have called it "the abused feature that irritates me most."

Remember back in the 80's when people were excitedly explaining why we need OO? The example I heard most was printing a circle. You don't have to know how to print a circle because the circle object knows how to print itself. It just means that instead of a straight forward piece of procedural code, the code gets split up and scattered all around in a bunch of tiny little illogical functions that you have to go find when you're debugging.

Actually I think it was SUPPOSED to be the hallmark of C++, it just didn't work out that way in the hands of a million line of business coders over the years.

C++ code was supposed to be self explanatory, read like a book, no need for comments, ever!

Problem is most app developers don't get to work in the confines of a single, thin, tier. Chances are if you needed to draw a circle, you had to implement some of that circle as well (bad example, but you get the idea)

Most app developers were working all up and down the app architecture in C++. Disk IO, UI work, DB calls, all from the same class. Don't blame them, they had a job to do and they needed to get from point A to point B.

Granted, people still do this in C#/Java/everything. But at least the newer languages were made with this experience in mind.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
13
81
www.markbetz.net
Almost wish I hadn't been on vacation for this one.

Eh... no I don't.

Neither of these languages is general purpose. Both are quite tricky to use, and highly unreliable if you don't know what you are doing.

I'm interested in why you think C and C++ are not "general purpose" languages.

Not sure what to make of the second sentence. Can a language be "unreliable?"
 

Pia

Golden Member
Feb 28, 2008
1,563
0
0
I'm interested in why you think C and C++ are not "general purpose" languages.
They have ceased to be general purpose languages, because nine times out of ten something like C# would beat them for writing a random piece of software. Someone who rationally chooses to use C or C++ in 2013 has specific reasons like particular kind of compatibility, low-level control, or high performance in mind. Why else would you tolerate all the bullshit and poor productivity that comes with these languages?
Not sure what to make of the second sentence. Can a language be "unreliable?"
If we're nitpicking, I guess not. C and C++ will reliably deliver undefined behavior and implementation-defined behavior all day long. What they don't do is to make it easy to write programs you can rely on.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
13
81
www.markbetz.net
They have ceased to be general purpose languages, because nine times out of ten something like C# would beat them for writing a random piece of software. Someone who rationally chooses to use C or C++ in 2013 has specific reasons like particular kind of compatibility, low-level control, or high performance in mind. Why else would you tolerate all the bullshit and poor productivity that comes with these languages?
If we're nitpicking, I guess not. C and C++ will reliably deliver undefined behavior and implementation-defined behavior all day long. What they don't do is to make it easy to write programs you can rely on.

Ok, I buy that they are no longer used for as wide a variety of tasks as they once were, but I think that is different from saying that they are not inherently general purpose languages.

On the reliability point, yes, they are much harder to write reliable software in. That's not really debatable.
 

Pia

Golden Member
Feb 28, 2008
1,563
0
0
Ok, I buy that they are no longer used for as wide a variety of tasks as they once were, but I think that is different from saying that they are not inherently general purpose languages.
Dunno who you are arguing now; I haven't said anything about "inherently general purpose languages". Nor am I going to, because that combination of words has no meaning.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
13
81
www.markbetz.net
Dunno who you are arguing now; I haven't said anything about "inherently general purpose languages". Nor am I going to, because that combination of words has no meaning.

Well, I think they do. There are specialized languages for specific purposes, such as PLC ladder logic and SQL. And there are general purpose languages that can express any logic on any processor for which they are implemented. Those include C and C++.
 

Pia

Golden Member
Feb 28, 2008
1,563
0
0
Well, I think they do. There are specialized languages for specific purposes, such as PLC ladder logic and SQL. And there are general purpose languages that can express any logic on any processor for which they are implemented. Those include C and C++.
"Can express any logic" equals "Turing complete". Is that what you mean when you say "general purpose language"?

Interestingly, a quick web search tells me a typical PLC ladder logic implementation actually is Turing complete and can therefore express any logic. Not easily, but still. So are Minecraft levels and so are C++ templates. Are these also "general purpose languages" to you?

Also, you said "inherently general purpose language" has an actual meaning and isn't just a bundle of silliness. If so, can you give an example of a non-inherently general purpose language? What's the difference between the two?
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
13
81
www.markbetz.net
"Can express any logic" equals "Turing complete". Is that what you mean when you say "general purpose language"?

Interestingly, a quick web search tells me a typical PLC ladder logic implementation actually is Turing complete and can therefore express any logic. Not easily, but still. So are Minecraft levels and so are C++ templates. Are these also "general purpose languages" to you?

Also, you said "inherently general purpose language" has an actual meaning and isn't just a bundle of silliness. If so, can you give an example of a non-inherently general purpose language? What's the difference between the two?

Turing completeness is probably too technically specific a definition, and in any case if I start talking about that then I'll sound like an idiot. A computer scientist I am not.

In my view an "inherently general purpose" language is one that was designed for writing many different kinds of applications, and comes with the library and tool support to do that. A "special purpose" language is one that was designed to solve a specific problem, like expressing database queries.

In any case, this debate doesn't have much value. What you clearly meant in your original post was "these languages are not widely used for general purpose development anymore," and that's probably true. Trying to transition from that observation to some characteristic of the languages themselves is meaningless. As Train said, they are clearly general purpose languages, however you want to define that term.
 

mv2devnull

Golden Member
Apr 13, 2010
1,498
144
106
I know ... Both are very popular (the most popular?) computer programming languages.

So in what situations would it be better to write in C++ as opposed to C, or vice-versa?
An assumption and a followup question.

The debate by Pia and Markbnj seems to make the assumption invalid at this timepoint. "Seems" as in how I interpret the meanings of "popular", "used", and "useful".

That generates a template for the answer to the question:
Code:
for situation X
    [C++|C] would be slightly better than [C|C++], because Y
    BUT language Z is even better
A practical answer should then list (X,Z) pairs, even though it does not formally answer the question.

A lot of HPC was/is Fortran.

Matlab, R, SQL, Python, are known to call C-binaries for heavy subprocedures, for whatever reason. One can either call external binary or a routine in a library. The latter is more efficient, but the library must present symbols that the caller can bind to.

The source of GCC used to be C-only, but latest version is compiled with C++. However, those are not the only languages in that collection.