C++ vs C

This is a discussion on C++ vs C within the General Discussions forums, part of the Community Boards category; I posted an admittedly strongly worded statement about the superiority of C++ over C. It seems I've greatly upset at ...

C++ vs C

I posted an admittedly strongly worded statement about the superiority of C++ over C. It seems I've greatly upset at least one person with that, and it deserves some explanation.

Originally Posted by gratiafide

For Brewbuck to say C is worthless seems a bit limited in perspective, I think that's all Nominal was really trying to get across in a very strong manner (since he loves C no doubt). But that's all his "religion" was trying to say, I think. Forgive me if I'm wrong.

I didn't say C was worthless, I said it was inferior to C++. "Worthless" would imply a comparison against all other programming languages. I am talking only of C and C++. Here's my fundamental argument in support of my opinion:

Any C program which is suitable for solving a given problem, is also a C++ program which solves that problem.

In other words, for any C program you can write, I can write a C++ program which is "equivalent enough" that you cannot possibly argue that C++ worsens anything. Usually, the way I'll rewrite it in C++ is to change the file extensions from .c to .cpp and be done with it (modulo some minor differences, which if you really insist, we can go into detail to show that they don't matter much).

So, it's not really hard to defend the idea that C++ is at least as good as C for any given problem, because any C program is (again, possibly with minor modifications) also a C++ program.

Given that, we could then start debating the marginal value of each of the features of C++ that aren't in C. But here's the point: even if the marginal value of every single feature of C++ over C is negative, I can still be just as good as C by taking a C program and calling it a C++ program (which it is).

"A C++ program can be just a C program, so at the very least it's equal to C - but it's probably better because it's got more."

Or, more clearly put:

Code:

(C++) >= C

A philosophical question: If you write a C program and compile it as a C++ program - is it really a C++ program? I mean, the machine language cares nothing for the source, there's no inherent "C++ brand" on the result. So I submit that it's not a C++ program, if you only used the syntax of C and used none of the features available in C++. That would be C, no matter how the compiler processes it.

So that would bring us to:

Code:

(C++) > C

Is this true? I don't know. I personally have a strong preference for C, but that's all it is - a personal preference. It's old, it's dangerous, it's syntactically simple and strangely beautiful.

But your argument so far only touched on the '=' part, not the '>'. So I'll stop here.

It seems I've greatly upset at least one person with that, and it deserves some explanation.

Unfortunately, the person that you upset decided to stick his fingers into his ears and tell you that he's not listening

Originally Posted by Matticus

If you write a C program and compile it as a C++ program - is it really a C++ program?

Yes: a valid C program can also be a valid C++ program.

Originally Posted by Matticus

I mean, the machine language cares nothing for the source, there's no inherent "C++ brand" on the result. So I submit that it's not a C++ program, if you only used the syntax of C and used none of the features available in C++. That would be C, no matter how the compiler processes it.

By the same reasoning, it is not a C program either: there's no inherent "C brand" on the result, since the result is assembly (and/or machine language).

Originally Posted by Matticus

But your argument so far only touched on the '=' part, not the '>'.

That is what I think too. It is similiar to the argument that a human assembly language programmer can always write code at least as efficient as what an optimising compiler can produce: in the worst case, he/she can just experiment with the compiler to find out what it does, then do the same thing.

Of course, it is not hard to show that some C++ features are improvements over their C near-equivalents (the upset guy mentioned exceptions, for example, and personally RAII is the reason why I would normally choose C++ over C), but they may come with trade offs, and showing that they are hence always better on balance is not that simple, I suspect.

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

I don't think that there is anything to be gained out of this conversation.

If we were trying to come to a consensus, I'd agree. But there is usually some insight or opinion that challenges our beliefs in these kinds of discussions. So I'd say, there's probably something to be gained.

That just proves that C is a subset of C++. Since the opposite is not true the ">=" seems pretty fair to me.

I'm not disagreeing with you - but what criteria are you using to determine the ">="?

Originally Posted by C_ntua

The question is really: if you had a C++ and a C compiler for a given device, is there a reason to prefer to start your project in C rather than C++?

Or, to put it another way, is there a reason to prefer to start your project in C++ rather than C?

[edit] remove a mini-rant because I just noticed C_ntua already addressed it in their argument. I have no answer to why I would prefer C to C++, other than I know C - I'm not making a claim that one is better than the other, I'm just offering my perspective.

The Arduino is a subset of C++ - (You can't do everything you can do in C++). I don't think that the compiler is very efficient, so I'd never use it for more than a hobby project - I think that that is doing more harm than good to your cause! I used it for my final year project, and got a little experience with it's implementation of C++.

I'm thinking about starting to program .NET in C++, but for now I don't think that there is any reason for me to learn C++.

Being proficient in C is perfect for my job - I'll probably learn C++ properly one day, but for now I don't need to know it.

Originally Posted by C_ntua

The question is really: if you had a C++ and a C compiler for a given device, is there a reason to prefer to start your project in C rather than C++?

Of course, it is not hard to show that some C++ features are improvements over their C near-equivalents (the upset guy mentioned exceptions, for example, and personally RAII is the reason why I would normally choose C++ over C), but they may come with trade offs, and showing that they are hence always better on balance is not that simple, I suspect.

The thing is, if you don't see the need for a C++ feature, you don't have to use it. On the other hand, if you're writing in C and suddenly want a C++ feature, you can't have it.

Here's an example of a useful function that has nothing to do with object orientation, and just can't be done in C:

In C, the alternative is to either use a macro (thus causing multiple evaluation of the arguments, which is problematic if they have side effects), to write a different Max() implementation for every possible type you'd want to use Max() on, or to use a non-standard language extension like GCC statement expressions combined with a macro.

From the page you linked, those examples seem to mostly be reasons why you can't just compile legacy C code as C++ and expect it to work. That's true, but has nothing to do with how you pick a language to write code in the first place.

While I don't know enough about C to truly make a judgement as I have only done C++ and C#, I do have to laugh at the philisophical argument of if a C program is a C++ program. Because the moment some guy posts his post first post ever in the C++ forum with printf() and strcmp() his post gets shipped off to C board. So I do believe there is an obvious difference in the moderator's minds.

It will make me chuckle if some new member links this thread when someone complains he posted his C program on the wrong board.

While I don't know enough about C to truly make a judgement as I have only done C++ and C#, I do have to laugh at the philisophical argument of if a C program is a C++ program. Because the moment some guy posts his post first post ever in the C++ forum with printf() and strcmp() his post gets shipped off to C board. So I do believe there is an obvious difference in the moderator's minds.

It will make me chuckle if some new member links this thread when someone complains he posted his C program on the wrong board.

I don't think I've ever seen a post transferred solely because somebody called a few C functions. Personally, I have no issue with using C functions in C++ code, but I won't argue with moderators about it.

But really, this is a "no true Scotsman" sort of argument and therefore bogus.