Why would anybody use C over C++?

c++ c
Joe Calimari picture Joe Calimari · Jan 31, 2009 · Viewed 141k times · Source

Although people seem to like to complain about C++, I haven't been able to find much evidence as to why you would want to choose C over C++. C doesn't seem to get nearly as much flak and if C++ has all these problems why can't you just restrict yourself to the C subset? What are your thoughts/experience?

Answer

Adam Davis picture Adam Davis · Jan 31, 2009

Joel's answer is good for reasons you might have to use C, though there are a few others:

  • You must meet industry guidelines, which are easier to prove and test for in C.
  • You have tools to work with C, but not C++ (think not just about the compiler, but all the support tools, coverage, analysis, etc)
  • Your target developers are C gurus
  • You're writing drivers, kernels, or other low level code
  • You know the C++ compiler isn't good at optimizing the kind of code you need to write
  • Your app not only doesn't lend itself to be object oriented, but would be harder to write in that form

In some cases, though, you might want to use C rather than C++:

  • You want the performance of assembler without the trouble of coding in assembler (C++ is, in theory, capable of 'perfect' performance, but the compilers aren't as good at seeing optimizations a good C programmer will see)
  • The software you're writing is trivial, or nearly so - whip out the tiny C compiler, write a few lines of code, compile and you're all set - no need to open a huge editor with helpers, no need to write practically empty and useless classes, deal with namespaces, etc. You can do nearly the same thing with a C++ compiler and simply use the C subset, but the C++ compiler is slower, even for tiny programs.
  • You need extreme performance or small code size, and know the C++ compiler will actually make it harder to accomplish due to the size and performance of the libraries

You contend that you could just use the C subset and compile with a C++ compiler, but you'll find that if you do that you'll get slightly different results depending on the compiler.

Regardless, if you're doing that, you're using C. Is your question really "Why don't C programmers use C++ compilers?" If it is, then you either don't understand the language differences, or you don't understand compiler theory.