C and C++ Compatibility

List of topics:

Character Array Initialization Functions with an Empty Argument List
Character Constants Global Constant Linkage
Class and typedef Names Jump Statements
Class and Scope Declarations main() Recursion
const Object Initialization Names of Nested Classes
Definitions Pointers to void
Definitions within Return or Argument Types Prototype Declarations
Enumerator Type Return without Declared Value
Enumeration Type __STDC__ Macro
Function Declarations typedefs in Class Declarations

Stroustrup also covers C and C++ Compatibility in Appendix B.


Character Array Initialization

In C++, when you initialize character arrays, a trailing ' \0' (zero of type char) is appended to the string initializer. You cannot initialize a character array with more initializers than there are array elements.

In C, space for the trailing '\0' can be omitted in this type of initialization.

The following initialization, for instance, is not valid in C++:

char v[3] = "asd"; // not valid in C++, valid in ISO C

because four elements are required. This initialization produces an error because there is no space for the implied trailing '\0' (zero of type char).


Character Constants

A character constant has type char in C++ and int in ISO C.


Class and typedef Names

In C++, a class and a typedef cannot both use the same name to refer to a different type within the same scope (unless the typedef is a synonym for the class name).

In C, a typedef name and a struct tag name declared in the same scope can have the same name because they have different name spaces. For example:

void main ()
{
     typedef double db;
     struct db; // error in C++, valid in ISO C

     typedef struct st st; // valid ISO C and C++
}

Class and Scope Declarations

In C++, a class declaration introduces the class name into the scope where it is declared and hides any object, function, or other declaration of that name in an enclosing scope. In C, an inner scope declaration of a struct name does not hide an object or function of that name in an outer scope. For example:

double db;
void main ()
{
     struct db               // hides double object db in C++
     { char* str; };
     int x = sizeof(db);     // size of struct in C++
                             // size of double in C
}

const Object Initialization

In C++, const objects must be initialized. In ISO C, they can be left uninitialized.

 


Definitions

An object declaration, for example:

int i;

is a definition in C++. In ISO C, it is a tentative definition.

In C++, a global data object must be defined only once. In ISO C , a global data object can be declared several times without using the extern keyword.

In C++, multiple definitions for a single variable cause an error. A C compilation unit can contain many identical tentative definitions for a variable.


Definitions within Return or Argument Types

In C++, types may not be defined in return or argument types. ISO C allows such definitions. For example, the declarations:

void print(struct X { int i;} x);      // error in C++
enum count{one, two, three} counter(); // error in C++

produce errors in C++, but are valid declarations in ISO C.


Enumerator Type

An enumerator has the same type as its enumeration in C++. In ISO C , an enumeration has type int.


Enumeration Type

The assignment to an object of enumeration type with a value that is not of that enumeration type produces an error in C++. In ISO C, an object of enumeration type can be assigned values of any integral type.


Function Declarations

In C++, all declarations of a function must match the unique definition of a function. ISO C has no such restriction.


Functions with an Empty Argument List

Consider the following function declaration:

  int f();

In C++, this function declaration means that the function takes no arguments. In ISO C, it could take any number of arguments, of any type.


Global Constant Linkage

In C++, an object declared const has internal linkage, unless it has previously been given external linkage. In ISO C, it has external linkage.


Jump Statements

C++ does not allow you to jump over declarations containing initializations. ISO C does allow you to use jump statements for this purpose.


main() Recursion

In C++, main() cannot be called recursively and cannot have its address taken. ISO C allows recursive calls and allows pointers to hold the address of main().


Names of Nested Classes

In C++, the name of a nested class is local to its enclosing class. In ISO C, the name of the nested structure belongs to the same scope as the name of the outermost enclosing structure.


Pointers to void

C++ allows void pointers to be assigned only to other void pointers. In ISO C, a pointer to void can be assigned to a pointer of any other type without an explicit cast .


Prototype Declarations

C++ requires full prototype declarations. ISO C allows nonprototyped functions.


Return without Declared Value

In C++, a return (either explicit or implicit) from main() that is declared to return a value results in an error if no value is returned. A return (either explicit or implicit) from all other functions that is declared to return a value must return a value. In ISO C, a function that is declared to return a value can return with no value, with unspecified results.


__STDC__ Macro

The predefined macro variable __STDC__ has the integer value 0 to indicate that C++ does not conform to ISO C. In ISO C, __STDC__ has the integer value 1.


typedefs in Class Declarations

In C++, a typedef name may not be redefined in a class declaration after being used in the declaration. ISO C allows such a declaration. For example:

void main ()
{
     typedef double db;
     struct st
     {
          db x;
          double db; // error in C++, valid in ISO C
     };
}