[WPI] [cs2223] [cs2223 text] [News] [Notes] 

cs2223, D97/98 Differences Between C and C++

C++, as it's name implies, is an incremental improvement on C. Here are some of the differences.

Keywords and Operators

C++ has more reserved words, see the keywords list list, and more operators, see the Precedence and Associativity chart.

Function Prototypes

In early versions of C, function prototypes were optional, although most compilers warn if you don't use them. In C++, function prototypes are mandatory.

Comments

In C, comments are delineated by /* and */. They can span multiple lines of code:

/* This is the beginning of a comment.

And this is the end */

In C++, there are two types of comments. The C-type comment is still valid. In addition, anything from // to the end of a line is a comment.

x = 2 * y; // Anything after here to the line end is a comment
y +=3; /* But the beginning of this line is code */

Declarations

In C, all declarations have to come before the first executable line in a function or block.

int a, b = 3;
a = b * b;

In C++, a declaration has to come at or before first use, and can appear anywhere in a program.

int b = 2;
b++;
int a = b * b;

Explicit Typedef

In C, you had to use a typedef to create a new type of struct or enum:

struct complex {double real; double imaginary;}; /* now "struct complex" is defined */
struct complex a,b; /* declaration of two structs */
typedef struct complex complex;  /* now "complex" is defined */
complex c,d; /* declaration of two more structs */

In C++, the type definition is automatic:

struct complex {double real; double imaginary;}; // now "complex" is defined
complex a,b,c,d; // declaration of four structs

Reference

In C, a place in memory can be referenced by an identifier or a pointer (the address of the location):

int x, *y;
x = 10; /* put 10 into x */
y = &x; /* y contains the address of x */
*y = 15; /* put 15 into the place pointed to by y, which happens to be x */
(*y)++; /* x now contains 16 */
*(y++); /* note that this dereferences the next place after y */

In C++, there is an additional option, the reference:

int x; // create a place to hold an int
int &z = x; /* z now refers to x */
x = 10; // put 10 into y
z++; // x now contains 11

This allows a function to modify a value in the calling function:

void test1(int); // prototypes
void test2(int &);

void test1(int a) { a++; } // functions
void test2(int &b) { b++; }

void main(void)
	{
	int x;
	x = 10;
	test1(x); // x is not modified
	test2(x); // now x is incremented
	}

The only way to do this in C is to use a pointer to simulate "call by reference"

void test1(int); /* prototypes */
void test2(int *);

void test1(int c) { c++; } /* functions */
void test2(int *d) { (*d)++; }

void main(void)
	{
	int x, *y;
	x = 10;
	y = &x; /* y points to x */
	test1(x); /* x is not modified */
	test2(y); /* now x is incremented */
	test2(&x); /* x is incremented again */
	}

Memory Allocation

C uses the functions malloc() and free() to dynamically allocate and free memory. These, plus related functions are in stdlib.h. In C++ the operators new and delete can also be used to allocate and free memory. Note, the usages are not exactly equivalent.

Input/Output

In C, input/output is formatted using function in <stdio.h>.

int x;
printf("Please enter an integer: );
scanf("%d", &x);
printf("Thank you\n");

In C++, stream operators in <iostream.h> allow unformatted input/output

int x;
cout << "Please enter an integer: ";
cin >> x;
cout << "Thank you" << "\n";

Classes

This is one area in which C++ is substantially different from C. The differences are too great to summarize here.

--------------------
[WPI Home Page] [cs2223 home page]  [cs2223 text] [News] [Notes] 

Contents ©1994-1998, Norman Wittels
Updated 14Mar98