In what situation would C be a better language to work with than C++. I guess since I'm young and wasn't brought up on C like others were, so I have no clue why people tend to still use C these days when C++ is now available? What advantages does C offer that keep people still wanting to stick to it?
Both languages have their merits:
C
- Is a smaller, simpler language to learn and use.
- Works well for relatively small tasks. (Even a
from-scratch, special purpose web server is only
a few thousand lines)
- Is appropriate for (say) device drivers.
C++
Gives access to OO design and coding, and therefore
enables the complexity of really large systems to be
better managed.
One area where you have to write in C rather than C++ is the Linux kernel. But this is due to some slight bigotry on the part of the original author :-)
Here's a few reasons:
- Encumbrance: Many old systems are already programmed using C, rather than C++. In many cases, it's not practical to migrate. This is especially so since mature applications have found ways to deal with most of what C++ has to offer natively in C, even if it's not quite as pretty.
- Availability: Porting a C compiler to a new architecture is much easier than moving a C++ compiler there. Therefore C is a bit more ubiquitous, especially on embedded platforms.
- Simplicity: C++ is a big language, and may be a little too much for some folks to choose to deal with. C, by comparison, is much simpler.
- Size: Although you can make a C++ program small by avoiding the features that lead to larger binaries, you end up missing out on most of the benefit of C++, may as well use C
- Faster Compile: The long time it takes for C++ programs to compile, especially in heavily templated code, may obstruct the development cycle and lead a team to a faster compiling C implementation.
Any area such as embedded systems which involves limited resources and code with a lot of low-level processes would be best done in C.
C is a small language. You tend to use about 80% of the features all the time. C++ is obscenely huge. You tend to use 20% of the features all the time.
C is still used a lot in device drivers. And on embedded systems (try out a few templates there and you'll know), and so on. You just can't afford a beast like C++ when you have constraints. I also know of projects where part of the product is created in C++ to be run on a host machine and part in C to be run on some custom hardware. Somehow, we haven't quite been successful in killing it ;-)
Personal experience: we had a custom templatized parser to extract numbers out of a stream. All using streambufs and what not. Along comes the client with a very, very complex file. What do we do -- we strip all that template down (a couple of functions), go back to the getchar()
way of life -- and voila! Not to say this not supported by C++, but just to highlight that the C way can be very, very effective at times.
I assume when most people ask this question they're referring to using C89 rather than C99. Many of the reasons mentioned, such as portability and lack of C++ compilers on certain platforms can most likely be extended to C99 as well.
If you are able to use C++, I think you should. This doesn't imply using object orientation or generic programming. If you're considering C89, I'd suggest that you should also consider using a subset of C++ or C99. This is because C++ and C99 both have a number of improvements over traditional C that are unrelated to OO and generic programming.