Previous Next Contents Index Doc Set Home


Moving From C to C++

7


This chapter describes how to move programs from C to C++.

C programs generally require little modification to compile as C++ programs. C and C++ are link compatible. You don't have to modify compiled C code to link it with C++ code.

See The C++ Programming Language, by Margaret A. Ellis and Bjarne Stroustrup, for more specific information on the C++ language.


Reserved and Predefined Words

Table 7-1 shows all reserved keywords in C++ and C, plus keywords that are predefined by C++. Keywords that are reserved in C++ and not in C are shown in boldface.



Table  7-1 Reserved Keywords  

asm

do

if

return

typedef

auto

double

inline

short

typeid

bool

dynamic_cast

int

signed

union

break

else

long

sizeof

unsigned

case

enum

mutable

static

using

catch

explicit

namespace

static_cast

virtual

char

export

new

struct

void

class

extern

operator

switch

volatile

const

false

private

template

wchar_t

const_cast

float

protected

this

while

continue

for

public

throw

default

friend

register

true

delete

goto

reinterpret_cast

try

__STDC__ is predefined to the value 0. For example:

Code  Example  7-1     Predefines
#include <stdio.h>
main()
{
    #ifdef __STDC__
        printf("yes\n");
    #else
        printf("no\n");
    #endif

    #if    __STDC__ ==0
        printf("yes\n");
    #else
        printf("no\n");
    #endif
}

produces:

yes
yes

The following table lists reserved words for alternate representations of certain operators and punctuators specified in the current ANSI/ISO working paper from the ISO C++ Standards Committee. These alternate representations have not yet been implemented in the C++ compiler, but in future releases may be adopted as reserved words and should not be otherwise used.



Table  7-2 Reserved Words for Operators and Punctuators

and

bitor

not

or

xor

and_eq

compl

not_eq

or_eq

xor_eq

bitand


Data Types

The basic (Solaris) C and C++ datatypes and their sizes are: (The size of each numeric type may vary among vendors.)

Each of char, short, int, long, and long long can be prefixed with signed or unsigned. A type specified with signed is the same as the type specified without signed, except for char and bitfields.


Creating Generic Header Files

K&R C, ANSI C, and C++ require different header files. To make C++ header files conform to K&R C and ANSI C standards so that they are generic, use the macro _ _cplusplus to separate C++ code from C code. The macro _ _STDC_ _ is defined in both ANSI C and C++. Use this macro to separate C++ or ANSI C code from K&R C code.

You can insert an #ifdef statement in your code to conditionally compile C++ or C using the C++ compiler. To do this, use the __cplusplus macro:

#ifdef __cplusplus 
int printf(char*,...);// C++ declaration 
#else 
int printf();/* C declaration */ 
#endif 


Note - In the past, this macro was c_plusplus, which is no longer accepted.


Linking to C Functions

The compiler encodes C++ function names to allow overloading. To call a C function or a C++ function "masquerading" as a C function, you must prevent this encoding. Do so by using the extern "C" declaration. For example:

extern "C" { 
double sqrt(double); //sqrt(double) has C linkage 
    } 

This linkage specification does not affect the semantics of the program using sqrt(), but simply causes the compiler to use the C naming conventions for sqrt().

Only one of a set of overloaded C++ functions can have C linkage. You can use C linkage for C++ functions that you intend to call from a C program, but you would only be able to use one instance of that function.

You cannot specify C linkage inside a function definition. Such declarations can only be done at the global scope.




Previous Next Contents Index Doc Set Home