Thread: C++ vs C

  1. #16
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Click_here View Post
    I think that this topic will only prove that C++ has more features than C.

    Code:
    #define Max(x,y) ((x)>(y) ? (x) : (y))
    What do I do when I want to use a third party library and one of its header files stupidly defines a macro called Max?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  2. #17
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by brewbuck
    What do I do when I want to use a third party library and one of its header files stupidly defines a macro called Max?
    You'd have to change the language you are programming in to C++ :P
    Fact - Beethoven wrote his first symphony in C

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, I suppose you could undefine the macro...
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    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.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Click_here View Post
    You'd have to change the language you are programming in to C++ :P
    I admit that these one-liner examples are a little weak, because one can always say "Well, this particular thing will be a little warty, but overall things are fine." You can get along fine with C-style programming, but there are occasions where everything would be so much better, if only you had namespaces. Or templates. Or references. Or consts that work better.

    Heck, just the idea of an "iterator" is so powerful I have no idea how I'd write signal processing code without it. Here's an example.

    You want to implement a two dimensional FFT. In practice, this is done by performing one dimensional FFTs along all rows, then all columns (or vice versa). To support working with either rows or columns, you might think to add a "stride" parameter to control how the FFT will iterate across its input. That will work, but what about the row-wise case where you know the stride is one? It would be nice if we had an optimized implementation for that case, as it makes a significant performance difference (up to 30%).

    With C, you either come up with some macro weirdness, or you take the 30% hit, or you actually implement two different functions and therefore double your bug-fixing effort if a problem is ever discovered in the algorithm. In C++, you just implement FFT as a template which takes iterators to the input and output arrays. These iterators can then hide the mechanics of the stride parameter. By passing a bare pointer, or direct iterator, to the FFT function, you get optimized code assuming a stride of one, which is necessary to process the rows. For the columns, you can create a "striding iterator" class which automatically advances by the proper amount and does the operator[]() style indexing using the correct stride. The stride itself can also be a template parameter, for instance if you are always doing FFTs of size 128, that can be "hard coded" as a template parameter thereby opening the potential for more optimization.

    Speaking of iterators, you can build bounds-checking into them in a way that only affects performance when built in debug mode -- release builds would have no bounds checks.

    These one-off examples are not illustrating the point as well as I would like. It's not until you start looking at entire software systems that you really start to perceive the impact.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Lesshardtofind
    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.
    The reason for that is simple: the beginner probably does not understand that C and C++ are different programming languages, and thus it is likely that a C program that happens to be a C++ program and is posted in the C++ programming forum is really intended to be purely a C program. If it turns out that the beginner really intended it to be a C++ program, then of course C++ specific constructs could be suggested to improve the code. That we do this is a separate issue from the fact that some valid C programs are valid C++ programs (and of course you exaggerate: the presence of C-style I/O and string functions does not make a C++ program a valid C program).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    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.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Fact - Beethoven wrote his first symphony in C

  7. #22
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Click_here View Post
    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++
    Neither of your statements here is true. There is nothing that can be achieved in C that can't be achieved in C++, or vice versa.

    What is true is that some things may be achieved more easily in one language or the other. The trade-off is one of effort to achieve a specified result within given constraints (execution time, etc) not whether it is possible to achieve it.

    C++ templates can be used to implement a family of functions which must each be implemented individually in C. However, it is still possible to implement a family of functions in C. More effort, but the end result is the same.

    Mechanisms like C++ exceptions can be implemented in standard C making use of library functions like setjmp() and longjmp(). In fact, some of the first C++ compilers that implemented templates used such a scheme. That scheme is less than ideally efficient in practice, but still feasible.

    The effects achieved with the help of RAII in C++ can be achieved in C, simply by disciplined management of the lifecycle of resources. There are a number of techniques that C programmers use - effectively, albeit with more effort - to manage that.

    With enough effort it is possible to write a C compiler in C, a C++ compiler in C, a C compiler in C++, or a C++ compiler in C++. The worst case is a need for some bootstrapping (in all cases) if the compilers are to emit native code, or are to be used in a circular manner (eg if a C compiler needs to be able to build itself).

    In the end, I don't subscribe to the view that either C or C++ is superior to the other. Both are turing-complete and any specified programming task can be achieved using them. There are some tasks for which I would prefer idiomatic C, and others for which I would prefer idiomatic C++. But the difference is one of practical technical and engineering trade-offs for a task at hand, not one of inherent superiority of one language over the other.
    Last edited by grumpy; 12-05-2012 at 01:39 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #23
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    Of course I exaggerated as my comment was meant merely as a jest and not to be taken as a literal statement that those two functions define a c program. I am sure deep down u understood the humor I was seeing. I wasnt intending to be making any sort of serious comment representing actual moderator behavior or truths of the lines between the language.

    I do understand the need to inform the new user of the correct language they are using. I guess I wasn't taking the debate as seriously as ya'll as they are all both obviously established languages and both can get u paid which would make it a profession. I see no point in debating if one profession is better than another as a victory would only lead to an implication that those who were of the other profession were now in some way inferior.

    Believeing oneself superior will lead to a severe learning disorder as one may now believe it unnecessary.
    Virtual reality hello world http://www.rodneybrothers.com/vr/vrh...rld/index.html in html and javascript.
    Viewable with dodocase, google cardboard, OR, and other compatible VR gear.

  9. #24
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    There is nothing that can be achieved in C that can't be achieved in C++, or vice versa.
    I was referring to not what can be achieved, but features included in the language, such as overloading functions
    Fact - Beethoven wrote his first symphony in C

  10. #25
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Click_here View Post
    I was referring to not what can be achieved, but features included in the language, such as overloading functions
    Well, you didn't make that clear. The words "do" and "achieve" are pretty closely related, and you did not post anything referring to feature sets.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  11. #26
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by grumpy View Post
    Well, you didn't make that clear. The words "do" and "achieve" are pretty closely related, and you did not post anything referring to feature sets.
    Appologies grumpy
    Fact - Beethoven wrote his first symphony in C

  12. #27
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by Click_here View Post
    I think that this topic will only prove that C++ has more features than C.

    Code:
    #define Max(x,y) ((x)>(y) ? (x) : (y))
    That very macro has caused a lot of hard-to-find bugs over the years.
    Code:
    int highestInsertPosition = 0;
    for (auto item : items) {
      highestInsertPosition = Max(highestInsertPosition, list->insertItemSortedAndReturnPosition(item));
    }
    list->select(highestInsertPosition);
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #28
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by grumpy View Post
    Neither of your statements here is true. There is nothing that can be achieved in C that can't be achieved in C++, or vice versa.
    On the contrary, this is a valid statement in a C program:

    Code:
    int class = 0;
    But i think you'll find that most C++ compilers will have a problem with that.

    ....i'll show myself out.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  14. #29
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    Well, one bit of stupidity deserves another...

    Code:
    #define class Class
    int class = 0;
    Soma

  15. #30
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Thank you phantomotap. Good way, I hope, to stop with the one-liners.

    I'll be forever surprised as to why is so difficult to accept that "C++ is a better C". And why some people think this means C is worse than C++.

    In any case, I'm here to tell everyone that C# is a better Java.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed