C in C++

To a large extent, C++ is a superset of C, and most carefully written ANSI C will compile as C++. There are a few major caveats though:

  1. All functions must be declared before they are used, rather than defaulting to type int.

  2. All function declarations and definition headers must use new-style declarations, e.g.,

    extern int foo(int a, char* b);
    

    The form extern int foo(); means that foo takes no arguments, rather than arguments of an unspecified type and number. In fact, some advise using a C++ compiler even on normal C code, because it will catch errors like misused functions that a normal C compiler will let slide.

  3. If you need to link C object files together with C++, when you declare the C functions for the C++ files, they must be done like this:

    extern "C" int foo(int a, char* b);
    

    Otherwise the C++ compiler will alter the name in a strange manner.

  4. There are a number of new keywords, which you may not use as identifiers --- some common ones are new, delete, const, and class.