Chapter 28. From C to C++

No distinction so little excites envy as that which is derived from ancestors by a long descent.

—François de Salignac de la Mothe Fénelon

C++ was built on the older language C, and there’s a lot of C code still around. That’s both a blessing and a curse. It’s a curse because you’ll probably have to deal with a lot of ancient code. On the other hand, there will always be work for you. This chapter describes some of the differences between C and C++, as well as how to migrate from one to the other.

K&R-Style Functions

Classic C (also called K&R C after its authors, Brian Kernighan and Dennis Ritchie) uses a function header that’s different from the one used in C++. In C++ the parameter types and names are included inside the ( ) defining the function. In Classic C, only the names appear. Type information comes later. The following code shows the same function twice, first as defined in C++, followed by its K&R definition:

int do_it(char *name, int function)   // C++ function definition
{
    // Body of the function

int do_it(name, function)             // Classic C definition
char *name;
int function;
{
    // Body of the function

When C++ came along, the ANSI C committee decided it would be a good idea if C used the new function definitions. However, because there was a lot of code out there using the old method, C accepts both types of functions. C++ does not.

Prototypes

Classic C does not require prototypes. In many cases, prototypes are missing from C programs. ...

Get Practical C++ Programming, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.