C and C++ compatibility on the z/OS platform

This information pertains to the differences between C and C++ that apply specifically to the z/OS® platform. The contents describe the constructs that are found in both ISO C and ISO C++, but which are treated differently in the two languages.

String 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 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).

Class/structure and typedef names

In C++, a class or structure 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 or structure 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:
int main ()
{
     typedef double db;
     struct db; /* error in C++, valid in C */

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

The same distinction applies within class/structure declarations. For example:

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

Class/structure and scope declarations

In C++, a class declaration introduces the class or structure name into the scope where it is declared and hides any object, function, or other declaration of that name in an outer 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;
int 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 C, they can be left uninitialized.

Definitions

An object declaration is a definition in C++. In C, it is a declaration (also known as a tentative definition). For example:

int i;

In C++, a global data object must be defined only once. In 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 declarations for a variable.

Definitions within return or argument types

In C++, types may not be defined in return or argument types. C allows such definitions. For example, the following declarations produce errors in C++, but are valid declarations in C:

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

Enumerator type

An enumerator has the same type as its enumeration in C++. In 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 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. 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 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 C, it has external linkage.

Jump statements

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

Keywords

C++ contains some additional keywords not found in C. C programs that use these keywords as identifiers are not valid C++ programs:

Table 1. C++ keywords
bool
catch
class
const_cast
delete
dynamic_cast
explicit
export
false
friend
inline
mutable
namespace
new
operator
private
protected
public
reinterpret_cast
static_cast
template
this
throw
true
try
typeid
typename
using
virtual
wchar_t

main() recursion

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

Names of nested classes/structures

In C++, the name of a nested class is local to its enclosing class. In 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 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. C allows nonprototyped functions.

Return without declared value

In both C and C++, the function main() must be declared to return a value of type int. In C++, if no value is explicitly returned from function main() by means of a return statement and if program execution reaches the end of function main() (that is, the program does not terminate due to a call toexit(), std::terminate(), or a similar function), then the value 0 is implicitly returned. A return (either explicit or implicit) from all other functions that are declared to return a value must return a value. In 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__ is defined for C++, and it has the integer value 0 when it is used in an #if statement, indicating that the C++ language is not a proper superset of C, and that the compiler does not conform to C. In C, __STDC__ has the integer value 1.