Introduction to C Programming - C and C++

Chapter chap1 section 8

Although it is the intention of these notes to stick rigidly to the ANSI standard for C, it is quite possible you will be using a compiler, such as Turbo C, that is really a C++ compiler. This is possible because C++ is a strict superset of C which means that any C program should be acceptable to a C++ compiler. In fact some C++ compilers operate by first converting the C++ code into C. As well as the ideas associated with object-oriented programming, C++ introduces a few extra features that the C programmer should be aware of, these include an alternative syntax for comments, several extra keywords that cannot be used in normal programming and a very simple form of input and output known as stream IO.

The alternative comment syntax uses the symbol // to mark the start of a comment and the end of a line to indicate the end of a comment. This means that you cannot have multi-line comments in C++ and it also means that you cannot have a comment in the middle of a line of code. If you have ever programmed in assembler language you'll be familiar with this style of commenting. The problem of nested comments does not arise.

Of course, C++ compilers will still accept C style comments marked by symbols "/*" and "*/". You shouldn't mix the two styles in the same program.

Here is an example of the hello world program written using C++ commenting style.

//          A simple program to demonstrate
//          C++ style comments
//
//          The following line is essential
//          in the C++ version of the hello
//          world program
//
#include	<stdio.h>
main()
{
	printf("hello, world\n");  // just like C
}

The C++ stream output facility is illustrated by the following program. The basic syntax of C++ stream output is

cout << value-to-display

Note that the program, like the previous example, requires the use of "#include" and a header file name. The meaning of "#include" will be discussed in a later chapter.

#include	<iostream.h>
main()
{
	cout << "hello, world\n";
}

The "<<" symbol is read as "puts to" but may also be read as "shift to". This is really C's left shift operator. C++ allows hexadecimal chraracter representations in strings unlike ANSI C.


Character Codes