Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I understand that C++ is a very fast language, but ain't C just as fast, or faster in some cases?

Then you might say that C++ has OOP, but the amount of OOP you need for most programming puzzles is not that big, and in my opinion C would be able handle that.

Here's why I am asking this: I am very interested in programming contests and competitions, and I am used to coding in C on those. However, I noticed that the vast majority of people use C++ (e.g., 17 out of 25 finalists on Google Code Jam 2011 used it, while no one used C), so I am wondering if I am at a disadvantage going with C.

Apart from the Object Orientation, what makes C++ a more suitable language for programming competitions? What are the features of the language I should learn and use to perform better on the competitions?

For background, I consider myself pretty proficient in C, but I am just starting to learn C++.

share|improve this question

closed as primarily opinion-based by gnat, MichaelT, Kilian Foth, World Engineer Oct 22 '13 at 1:03

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.If this question can be reworded to fit the rules in the help center, please edit the question.

3 Answers 3

up vote 44 down vote accepted

To start with, there will always be some problems that are better solved in one language than another. There will always be languages that solve specific problems "better" than any other language, for some definition of "better". However, a very very large number of problems have very similar needs (some I/O, some computation) and face similar requirements (reasonable reliability, reasonable performance).

As you know C already, for the vast majority of problems out there, I state that C++ provides no significant downsides and a number of significant improvements. Bold? Some people seem to think so, but it's really the case. Let's start out by clearing up a few very common C++ misunderstandings:

  • C++ is slower than C. Wrong! Many C programs are valid C++ programs as well - and such a C program should run at identical speed when compiled with either the C compiler or the C++ compiler.

  • C++ specific features require overhead. Wrong! The so-called overhead introduced by certain C++ specific features (such as virtual function calls or exceptions), is comparable to the overhead you yourself would introduce should you implement a similar feature in C.

  • C++ is object oriented. Wrong! The C++ language contains some language extensions that facilitate object oriented programming and generic programming. C++ does not force object oriented design anywhere - it merely allows it. C allows for object oriented programming as well, C++ only makes it simpler and less error-prone.

So, if you believe me, we have established that "C++ is not significantly worse than C". Let's have a look at what makes C++ a better C:

  • Stronger typing The type system in C++ is stronger than in C. This prevents many common programming errors - coupled with the next very important feature, the stronger type system even manages not to be an inconvenience.

  • Parameterized types The template keyword allows the programmer to write generic (type-agnostic) implementations of algorithms. Where in C, one could write a generic list implementation with an element like:

    struct element_t {
      struct element_t *next, *prev;
      void *element;
     };
    

C++ allows one to write something like:

template <typename T>
struct element_t {
   element_t<T> *next, *prev;
   T element;
};

Not only does the C++ implementation prevent common programmer errors (like putting an element of the wrong type on the list), it also allows better optimization by the compiler! For example, a generic sort implementation is available in both C and C++ -

the C routine is defined as:

void qsort(void *base, size_t nmemb, size_t size,
           int(*compar)(const void *, const void *));

whereas the C++ routine is defined as

template void sort(RandomAccessIterator first, RandomAccessIterator last);

The difference being, that for example sorting an array of integers, would, in the C case, require a function call for every single compare, whereas the C++ implementation would allow the compiler to inline the integer comparison calls, as the actual sort routine is automatically instantiated at compile time by the compiler, with the correct types inserted in the template arguments.

  • A bigger standard library C++ allows the full use of the C standard library. This is very important of course, as the C standard library is an invaluable resource when writing real world programs. However, C++ includes the Standard Template Library. The STL contains a number of useful templates, like the sort routine above. It includes useful common data structures such as lists, maps, sets, etc. Like the sort routine, the other STL routines and data structures are "tailored" to the specific needs the programmer has - all the programmer has to do is fill in the types.

Of course, the STL is no silver bullet - but it does provide a great help very often, when solving general problems. How often have you implemented a list in C? How often would an RB-tree have been a better solution, if only you had had the time to do it? With the STL you do not need to make such compromises - use the tree if it's a better fit, it's as easy as using the list.

Ok, so I've only been discussing the good parts. Are there any downsides? Of course there are. However, their number is shrinking day by day. Let me explain:

  • There are no good C++ compilers It's been like this for a long time. But you must remember, that the language was standardized in 1998 - it is a complex language, more complex than C. It has taken a long time for compilers to catch up to the standard. But as of this writing, there are good compilers available for the most widely used platforms out there; GCC in versions 3.X are generally very good, and it runs on GNU/Linux and most UNIX platforms. Intel has a good compiler for Win32 - it is also pretty good, but unfortunately it still relies on the MS STL which is sub-par.

  • People don't know good C++ This is not an often heard complaint, but it's something that I see a lot. C++ is a big and complex language - but it also used to be a language that was hyped a lot, especially back in the "OOP solves hunger, cures AIDS and cancer" days. The result seems to be that a lot of really poor C++ code, basically bad C with a few class declarations here and there, is out there and is being used as learning material. This means a lot of people who believe they know C++ actually write really crappy code. That's too bad, and it's a problem, but I think it's unfair to blame this on C++.

So, the only two major problems with C++ are results of C++ being a young language. In time they will vanish. And for most problems out there, if you can get good programmers (or learn good C++ yourself), the problems are not really an issue today.

share|improve this answer
6  
+1. Very complete answer. Only thing I have a different opinion is that in the future, the main downsides of C++ will vanish. Since C++ has to be backwards compatible, there will almost no language features be removed from C++, only new ones added (C++11 is a perfect example for this). That will make the language even more complexer than it is today, which is IMHO the biggest downside of C++. –  Doc Brown Dec 16 '11 at 13:06
    
@DocBrown: That depends on how you use C++. If you're working with lots of older code, you need to understand how that works, and therefore probably need a wide knowledge of C++. If you're just writing new code (such as in a competition), you can confine yourself to just what you're going to use, avoiding a lot of cruft (like, say, auto_ptr<>). –  David Thornley Dec 16 '11 at 16:55
    
Great answer, but I think "Many C programs are valid C++ programs as well" isn't quite strong enough as the differences don't change code generation. Nearly every C program could be rewritten as an identically performing valid C++ program with relatively little effort. –  Steven Burnap Oct 19 '13 at 21:28

Contests like that are not so much about the speed of the program as the speed of the programmer. C++ has standard library features, type safety, and help for memory management that makes developing and debugging faster, even if the executable ends up slightly slower.

share|improve this answer

Speaking as a previous Code Jam finalist, it's mostly about the libraries rather than the language features. Competition solutions seldom use any OOP design principles, but you're likely to see a tour of most of the standard library containers and algorithms - string, vector, list, stack, queue, deque, priority_queue, set, map, complex, pair, bitset, lower_bound, reverse, sort, find, count, nth_element, min, max, min_element, max_element, unique, next_permutation, ... skilled contestants will be familiar with all of them and gain a lot of time by not having to implement and debug them in C.

Code Jam allows contestants to bring in their own code and use third-party libraries, so in theory a contestant could have this all pre-implemented in C. However, not all contests allow this, and templates and operator overloading make this much more readable than in C.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.