The C++ recipe

ANSI C insists on very strict rules of syntax. These include the order in which things are written. As we shall see, C++ is stricter about some things and more relaxed about others. Here is a correct C++ program.
#include <iostream.h>

		 // This is a very simple C++ program 

	int main()
	{
		cout << "Success\n";
		return 0;
	}
It is very straight forward, but it shows several important differences to ANSI C.

Firstly, the #include directive now brings in the C++ iostream.h library instead of C's stdio.h. In practice most compilers will accept ANSI C header files and stdio.h features can still be used, but we shall not be doing so here.

Second, the C++ comment delimiter is used. This is a double slash (oblique), //, at the beginning of the comment. It marks the remainder of the line as to be ignored. It does not continue across lines. C++ compilers will also recognise the C style of bracketted comment.

Functions like main are declared in the same way as in C. main is always expected to be of type int in C++, however.

The output statement now uses the standard output stream, cout, and the output operator <<. This is a much simpler way to achieve I/O than in ANSI C.

In general C++ is a superset of ANSI C, but programmers tend to use its new versions of certain features in preference to ANSI C features. There are a couple of small incompatibilities, as we shall see elsewhere in these notes.

Next note in series

Back to index