Wednesday 16 November 2011

What is the difference between C++ and C? in C programming

What is the difference between C++ and C?

There are two perspectives to consider: the C programmer’s, and the C++ programmer’s.

To a C programmer, C++ is a quirky language that’s hard to deal with. Most C++ libraries can’t be linked into a C program by a C compiler. (There’s no support of templates or “virtual tables,” which the compiler has to create at link time.) Even if you link your program with a C++ compiler, a lot of C++ functions can’t be called at all from C code. C++ programs, unless they’re very carefully designed, can be somewhat slower and a lot bigger than similar C programs. C++ compilers have more bugs than C compilers. C++ programs are much harder to port from one compiler to another. Finally, C++ is a big language, hard to learn. The definitive (for 1990) book was more than 400 pages long, and more has been added every year since then.

C, on the other hand, is a nice, simple language. No changes have been made to the language in years. (That won’t last forever; see FAQ XV.1.) The compilers are good and getting better. Good C code is trivial to port between good C compilers. Object-oriented design isn’t easy to do in C, but it’s not that hard. You can (almost) always build your C code with C++ compilers if you want.

To a C++ programmer, C is a good beginning. There are many mistakes you can make in C that you’ll never make in C++; the compiler won’t let you. Some of the tricks of the C trade can be very dangerous if just slightly misused.

C++, on the other hand, is a great language. With a little discipline and up-front design work, C++ programs can be safe, efficient, and very easy to understand and maintain. There are ways of writing C++ programs so that they will be faster and smaller than the equivalent C programs. Object-oriented design is very easy in C++, but you’re not forced to work that way. The compilers are getting better every day, and the standards are firming up. You can (almost) always drop down to the C level if you want to. What, specifically, is different between C and C++? There are a few C constructs that C++ doesn’t allow, such as old-style function definitions. Mostly, C++ is C with new features:

A new comment convention (see FAQ XV.3).

A new “Boolean” type with real true and false values, compatible with existing C and C++ code. (You can throw away that piece of paper taped to your monitor, the one that says, “0 = false, 1 = true.” It’s still valid, but it’s just not as necessary.)

Inline functions, safer than #define macros and more powerful, but just as fast. Guaranteed initialization of variables, if you want it. Automatic cleanup of variables when they go away.

Better, safer, stronger type checking and memory management.  Encapsulation, so new types can be defined with all their operations. C++ has a complex type, with the same operations and syntax as float or double. It’s not built into the compiler; it’s implemented

in C++, using features every C++ programmer can use.

Access control, so the only way to use a new type is through the operations it allows.  Inheritance and templates, two complementary ways of writing code that can be reused more ways than just calling functions.

 Exceptions, a way for a function to report a problem further than just the function that called it. A new approach to I/O, safer and more powerful than printf, that separates formatting from the kindof file being written to. A rich library of data types. You’ll never have to write a linked list or a binary tree again. (This tim for sure, honest. Really!) 

Which is better, C or C++? That depends on who you are, who you’re working with, how much time you have to learn, and what tools you need and want and can use. It depends. There are C++ programmers who will never go back to C, and C programmers who have gone back from C++ and love it. There are programmers who are using some C++ features and a C++ compiler, but who have never really understood C++, who are “writing C programs in C++.” Hey, there are people writing FORTRAN programs in C (and C++); they never caught on either. Great programming languages don’t make great programs. Great programmers understand the language they’re programming in, whatever language it is, and use it to make great programs.

Cross Reference:

XV.1: Should C++ additions to a compiler be used in a C program?

No comments:

Post a Comment