Learn C++ - C++ Introduction

Let's begin with a simple C++ program that displays a message.

The following code uses the C++ cout (pronounced "see-out") to produce character output.

The source code comments lines begin with //, and the compiler ignores them.

C++ is case sensitive. It discriminates between uppercase characters and lowercase characters.


The cpp filename extension is a common way to indicate a C++ program.

#include <iostream>                           // a PREPROCESSOR directive 
int main()                                    // function header 
{                                             // start of function body 
     using namespace std;                      // make definitions visible 
     cout << "this is a test.";  // message 
     cout << endl;                             // start a new line 
     cout << "hi!" << endl;   // more output 
     return 0;                                 // terminate main() 

The code above generates the following result.


To make the window stay open until you strike a key by adding the following line of code before the return statement:


C Input and Output

If you're used to programming in C, you would not know cout but you do know the printf() function.

C++ can use printf(), scanf(), and all the other standard C input and output functions, if that you include the usual C stdio.h file.

You construct C++ programs from building blocks called functions.

Typically, you organize a program into major tasks and then design separate functions to handle those tasks.

The example shown above is simple enough to consist of a single function named main().

The main() function is a good place to start because some of the features that precede main(), such as the preprocessor directive.

Note 2

The sample program has the following fundamental structure:

int main() 
     return 0; 

The final statement in main(), called a return statement, terminates the function.

The code above has the following elements:

  • Comments, indicated by the // prefix
  • A preprocessor #include directive
  • A function header: int main()
  • A using namespace directive
  • A function body, delimited by { and }
  • Statements that uses the C++ cout facility to display a message
  • A return statement to terminate the main() function

The Function Header

The following function header states that the main() function returns an integer value to the function that calls it and that main() takes no information from the function that calls it:

int main()

Many existing programs use the classic C function header instead:

main()     // original C style 

Under classic C, omitting the return type is the same as saying that the function is type int.

However, C++ has phased out that usage.

You can also use this variant:

int main(void)     // very explicit style 

Using the keyword void in the parentheses is an explicit way of saying that the function takes no arguments.

Under C++, leaving the parentheses empty is the same as using void in the parentheses.

Some programmers use this header and omit the return statement:

void main()