The best programs are written so that computing machines can perform them quickly and so that human beings can understand them clearly. A programmer is ideally an essayist who works with traditional aesthetic and literary forms as
well as mathematical concepts, to communicate the way that an algorithm works and to convince a reader that the results will be correct. Donald E. Knuth

Basic Syntax

C++ Program Structure
Let's look at the most basic C++ program :

/*
 * File : main.cpp
 * Author : csegeek
 * Description : My first C++ Program
 */

#include <iostream>
using namespace std;

// program execution starts at main()
int main() {
   cout << "Hello World!"; // prints "Hello World" 
   return 0;
}

When the program is run, it prints the following output :
Hello World!

Comments
Comments are a way of explaining what your program does. They can be single line ( put after // ) or multi line
( enclosed within /*      */ ). Comments are ignored by the compiler and are just a means for you and others to understand the piece of code.

< iostream > Header File
#include is a preprocessor directive which tells preprocessor to include the contents of iostream header file before compilation. iostream contains code that tells compiler what cout will do and ofcourse it contains code for other standard library functions. There are many other header files which we should be included as and when required.

Namespace
Namespace is a new concept introduced in C++. Here, std is the namespace where classes, objects & functions of standard C++ library is defined. using keyword will bring all contains of std namespace within the scope of the current program.

main ( )
The program execution begins at the main ( ) function. int (integer) specifies the return type of main ( ) function. Every program must have one and only one main ( ) function.

Output Operator ( << )
cout << causes the message " Hello World " to be printed on the screen. You will learn about other operators as you move along.

Return Type of main ( )
main ( ) returns an integer type value. The statement return 0; causes termination of the main ( ) function or the program and returns 0 ( integer ) to the Operating System.

Don't worry if you haven't understood some of the terms presented above. Everything will become crystal clear as you proceed further with the tutorial.

Back | Next