Home » C++ » C++ Basics

C++ Basics

In this tutorial, you will learn about C++ basics and some important notes on the main function.

C++ is a superset of the C language with extensions and improvements. Extensions are the new feature in the language, whereas improvements are better ways of doing that C already does. Below is an example of a very basic C++ program:


It is the smallest possible C program as well. Because C++ is a superset of the C language, you can use a C++ compiler to develop and compile C programs.

C++ Basics: Hello World Program

The above minimum program does not do anything. It contains no more than one function, which as in C, must be named main. Below is a C++ program that does something:


#include <iostream.h>
   cout << "Hello, world";

The hello.cpp program includes iostream.h and it uses an unfamiliar syntax with the undefined variable name cout, the bitwise shift left operator (<<), and the only familiar part of the example, a literal string expression that greets the world.

Important Note

The above examples declare the main function with no return type, which implies that the main returns an integer, yet the main functions in the examples have no return statement. The C++ language specification says that the main function type is implementation-dependent. Traditionally, the main returns an integer value, which the program returns to the system.

If the main returns no value, the compiler assumes a void return, and the system receives an undefined return value when the program terminates by a return from the main.

Some C++ compilers issue the warning. The Symantec C++ compiler issues an error and refuses to compile programs with main functions that are declared to return something but do not return anything.

C++ Basics: Input/Output Streams

The C++ compiler products use classes to implement an improved stream input/output system. That design has become a de facto standard in C++ programs and is a part of the proposed standard C++. You use these improved streams to read and display information.

Standard Output Stream

The cout variable, seen in the above example, is the C++ standard output stream, which writes to the console:

cout << "Hello, world";

The << operator is the output operator. It points symbolically from what is being sent to where it is going.

Standard Input Stream

For the input stream in C++, use the cin object with >> operator. Below is an example:

#include <iostream.h>
   int amount;
   cout << "Enter an amount...";
   cin >> amount;
   cout << "The amount you entered was " << amount;


Enter an amount...74
The amount you entered was 74

In the above program, it sends a string to cout to prompt you for input. The cin device writes the value that you enter into the amount integer variable. The exercise then displays the amount variable on cout.

Standard Error Stream

The cerr object uses the same syntax as cout, except that cerrs output goes to the standard error device. This technique allows you to display error messages on the console.

C++ Basics: Comments

C++ supports the standard C comment format. The /* character sequence begins a comment, and the */ sequence ends it. But C++ has another comment format. The C++ token is the double-slash (//) sequence. Wherever this sequence appears, everything to the end of the current line is a comment. Below is an example:

#include <iostream.h>
   /* C++ basics - comments example */
   char name[20]; // declare a name string
   cout << "Enter a name..."; // request a name
   cin >> name; // read the name
   // display the name
   cout << "The name you entered was " << name;

C++ Basics: Keywords

C++ reserves the standard C keywords and adds some of its own. The below list is having the standard C keywords and the common keywords that C++ adds. This tutorial is to just give the introduction of the C++ basics so I am not giving many details of each command for now. But in future posts definitely, you will be learning all about these keywords.

asm double new switch
auto else operator template
break enum private this
case extern protected throw
catch float public try
char for register typedef
class friend return union
const goto short unsigned
continue if signed virtual
default inline sizedof void
delete int static volatile
do long struct while


This post gave you the first exposure to the C++ basics. Mainly about the main function and how it works in the C++ program, I/O streams, comments, and C++ keywords.