Like Tree32Likes

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 ...

  1. #1
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239

    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.

    Quote Originally Posted by gratiafide View Post
    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).
    iMalc likes this.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    2,292
    Is this an accurate assessment of your argument?

    "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.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    Quote Originally Posted by brewbuck
    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

    Quote 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.

    Quote 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).

    Quote 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.
    Matticus likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    2,292
    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.
    I agree with the technicality.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        printf("I bought my love a chicken\n");
        printf("It had no bones!\n");
    
        return 0;
    }
    This can be either a 'C' or a 'C++' program. But if this was also you had to go on, what you would label it as? It's valid as a 'C++' program, but it's clearly a 'C' program.

    ...showing that they are hence always better on balance is not that simple, I suspect.
    I most definitely agree.

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,344
    I know that there is a lot of things that you can do in C++ that you can't do in C.

    There may be things you can do in C, which you can't do in C++

    They are different.

    I don't think that there is anything to be gained out of this conversation.
    Fact - Beethoven wrote his first symphony in C

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    2,292
    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.

  7. #7
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by Matticus View Post
    This can be either a 'C' or a 'C++' program. But if this was also you had to go on, what you would label it as? It's valid as a 'C++' program, but it's clearly a 'C' program.
    That just proves that C is a subset of C++. Since the opposite is not true the ">=" seems pretty fair to me.

    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++?

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    2,292
    Quote Originally Posted by C_ntua View Post
    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 ">="?

    Quote Originally Posted by C_ntua View Post
    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.
    Last edited by Matticus; 12-04-2012 at 09:52 PM.

  9. #9
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,344
    Here is what I posted in the last thread


    Quote Originally Posted by click_here
    There is still a big part of Microcontroller programming done in C.


    ...


    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.



    Quote 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++?
    The answer is that there are situations where you can not use C++.

    Here is some differences Compatibility of C and C++ - Wikipedia, the free encyclopedia
    Fact - Beethoven wrote his first symphony in C

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    2,292
    Regarding your first quote: I nearly misunderstood the argument upon reading, but C_ntua already addressed it:

    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++?
    *emphasis mine
    It was more of a general question and not, I believe, intended to be practical in nature.

    Regarding your second quote - nice find!

  11. #11
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by laserlight View Post
    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:

    Code:
    template <typename T>
    T Max(T a, T b)
    {
        if (a >= b) return a;
        return b;
    }
    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.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  12. #12
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by Click_here View Post
    The answer is that there are situations where you can not use C++.
    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.

    "I wish I could use C++, but sizeof('a') == 1 and my entire design falls apart if that's true!" Sounds crazy.

    "I wish I could use C++, but it won't let me use a goto statement to skip the declaration of a variable then use the variable!" Sounds crazy.

    "I wish I could use C++, but it won't allow me to declare a struct type inside the prototype of a function!" Sounds crazy.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  13. #13
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    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.

  14. #14
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by Lesshardtofind View Post
    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.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  15. #15
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,344
    I think that this topic will only prove that C++ has more features than C.

    Code:
    #define Max(x,y) ((x)>(y) ? (x) : (y))
    Fact - Beethoven wrote his first symphony in C

Page 1 of 6 123456 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21