Chapter Contents

Previous

Next
SAS/C C++ Development System User's Guide, Release 6.50


Differences between C and C++

The following sections outline the major differences between C and C++ and describe how to change the C constructs to their corresponding C++ constructs.


Reserved Keywords

The following keywords cannot be used as identifiers in C++ programs because they are reserved:

asm new this
catch operator throw
class private try
const_cast protected typeid
delete public typename
dynamic_cast reinterpret_cast virtual
friend static_cast
inline template

 

If these keywords are used as identifiers in your C program, you must change them to some other name.


Function Prototypes

In C++, a function must have a prototype in scope when it is called. In C, this is not necessary, although it is regarded as good programming practice. It is common in C code to find function declarations that are not prototypes (because frequently it is only the return type that is important to declare). For example, the following statement is regarded in C as a declaration but not a prototype of a function with no parameters:
int myfunc();
 

In C++, this same statement is interpreted as a prototype equivalent to the following:
int myfunc(void);
If you mistakenly use the  myfunc() form in your C++ program as a prototype and then call the function with one or more parameters, you will receive error messages. In converting a program from C to C++, you must change all of your nonprototypical function declarations to prototypes. 


"C" Linkage

If a C++ function calls C functions that are not to be converted to C++ functions (using the guidelines in this appendix), the declarations of these C functions in the C++ source (or header files) must include the extern "C" qualifier or be within an extern "C" block. For the standard SAS/C library functions, "C" linkage is specified in the SAS/C header files.


Multiple Declarations of File-Scope Variables

In ANSI C, a file-scope variable can be declared more than once without using the extern keyword. This is not legal in C++. To convert your program from C to C++, make sure that all but one declaration of a file-scope variable has the  extern keyword. If the file-scope variable is initialized, the initializer should be attached to the one declaration without the  extern keyword. 


Assigning Integers to enums

In C++, an integer cannot be assigned directly to an enum as it could be in C. If your program contains such assignments, first cast the integer to the  enum type, as in the following example:
int i;
enum X {a, b, c} e;

e = i; /* Legal in C, not in C++. */

e = (enum X)i; // Legal in C++.
Of course, the best approach is to use the correct  enum constant in the first place. 


Assigning void* Values to Pointers

In C++, a void* pointer can be assigned only to another  void* pointer. If your C program contains assignments of  void* pointers to other kinds of pointers, you should cast the  void* to the other pointer type before the assignment, as in the following example:
int* ip;
void* vp;
ip = vp; /* Legal in C, not in C++. */
ip = (int*)vp; // Legal in C++.
 


Using Strings to Intitialize a Character Array

In C++, a string used to initialize a character array must be at least one character shorter than the array it is initializing. The extra element is for the terminating null character. The following example shows how this works:
char a[3] = "SAS"; /* Legal in C, not in C++. */

   // Legal in C++, keeps the terminating null
   // character.
char a[4] = "SAS";

   // Legal in C++, omits the null-character.
char a[3] = {'S', 'A', 'S'};
 


Using File-Scope Constants

In C, file-scope constants are external by default. In C++, file-scope constants are static by default. You can take two approaches to converting this type of problem:  

 

Using #pragma linkage

The SAS/C extension #pragma linkage is supported only within  extern "C" linkage blocks in C++ programs. Programs that use the  #pragma linkage feature outside  extern "C" blocks should be changed to use the  __ibmos keyword. 

To change uses of #pragma linkage , remove the  #pragma linkage and add  __ibmos to the declaration of the function that needs IBM OS linkage. The following example shows equivalent C and C++ usage:
   /* Meaningful only in C. */
#pragma linkage (fname, OS)
int fname();

   // Meaningful in both C and C++.
__ibmos int fname();
 

__ibmos functions always have "C" linkage. For more information on the  __ibmos keyword, refer to the SAS/C Compiler and Library User's Guide, Fourth Edition. 


Identical Class Names and typedef Names

In C++, if a class name and a typedef name are the same, the  typedef name must refer to the class. If your C program contains a class (structure) name and a  typedef name that are the same, but refer to different things, you must change one of the names. Here is an example:
  /* This code is legal in C, but not in C++,
     because VAL refers to an int and a struct. */
typedef int VAL;
struct VAL
{
long x;
char *text;
};

  // This code is legal in both C and C++,
  // because VAL refers to the same thing in each
  // case.
struct VAL
{
long x;
char *text;
};

typedef struct VAL VAL;
 


Embedded Structure Tags

In C, a structure tag can be defined inside the definition of some other structure tag. This is also legal in C++, but because structures (classes) are scopes in C++, the inner structure tag is hidden inside the outer structure. If you do not want this scoping effect, do not embed structure tags. (In other words, move the inner structure's definition outside the outer structure's definition, or use the scope operator ( :: ) to use the inner structure's tag in subsequent instantiations, as in  outer::inner .) 


Using goto Statements

C++ does not allow you to use the goto statement to jump over an initialization. If your C program contains such jumps, you must remove them. 


Differing Types

C++ defines the types of character literals and enumerations differently than C does.

Character literals

In C, the type of a single-character character literal (for example, ' H ') is  int . In C++, character literals are of type  char . You do not usually have to worry about this distinction, unless your program expects the result of  sizeof('H') to be greater than one. 

Enumerations

In C, the type of an enum is  int . In C++, enumerations have type  enum . Again, you do not usually have to worry about this distinction; just remember that the size of an  enum may be different in C and in C++.


Chapter Contents

Previous

Next

Top of Page

Copyright © Tue Feb 10 12:11:23 EST 1998 by SAS Institute Inc., Cary, NC, USA. All rights reserved.