Syntax and Structure of C++ program

Basics of C++ Syntax and Structure of C++ program

In this section, we will discuss some basic rules and syntax of C++ program. If you are familiar with C language then you will find the syntax very similar to C language. If you have never used C or any other computer languages then don’t worry, we will discuss each and every thing in detail in these tutorials. If you are already familiar with a computer language then of course it will be easier for you to learn the syntax of C++ program.

Like most other computer languages, C++ is a case sensitive language which means that you must be very careful while writing C++ statements. If you are supposed to write “include” for example, then it must be written exactly the same including spelling and case. Lower case characters must be written in lowercase and uppercase letters must be written in uppercase. A good thing is that most of the code is written in lowercase so just keep ‘caps lock’ button on the keyboard off while writing the code.

If you don’t follow the rules of C++ language then you will get errors from compiler. Compiler is software used to translate the C++ code into machine language. It is important to note that your C++ code is not directly understood by the computers.

Computers can understand only machine language (composed of 0s and 1s). So you need to convert your C++ code into machine language to make it understandable by the computers. For this purpose, we use a translator. The original code written in a non machine language is called source code. When the translator converts this code into machine language, then the corresponding code is called object code. Hence we can say that translator converts the source code into object code as shown in the figure below.

source code

There are several kinds of translators but we usually use compilers for this purpose. Compiler is a kind of translator which reads the whole program and check for the syntax errors present in the program. If you have not followed a rule of the language then it is called syntax error.

Complier displays a list of the syntax errors present in the program. You then need to rectify these errors. Your code will not be converted into machine language unless all the syntax errors are removed. This process of removing syntax errors from your source code is sometimes called debugging.

In order to write, compile and execute the C++ program, you need an IDE (Integrated Development Environment). There are many free and commercial IDE software for C++ is available. For example you can use Dev C++, Eclipse or MS Visual Studio C++ for this purpose.

Syntax and Structure of C++ program

Let us consider a very basic C++ program to show the syntax and structure of a C++ program.

Syntax and Structure of C++ program

The output of the program is as below.

output of the program

Let us discuss the program line by line. Kindly note that line numbers given in the above program are just for explanatory purpose. They are not part of the program.

Line 1:   It shows the comments. Comments are used to explain a specific part of the program to improve the understanding. Comments are not part of the program and are never compiled or executed. Comments start with // if they are single line. Anything after // are considered as comments till end of line. We can write multiline comments too. Multiline comments start with /* and end with */

Line 2:   C++ consists of a number of predefined functions. A function is a piece of code to perform specific task. The predefined functions are stored in header files. “iostream” is one of the predefined header files having necessary functions related to input and output streams. We put a # sign before a header file to include it. # is called preprocessor directive. It makes the header file part of the program even before compilation. In line 2, we include “iostream” header file. This is required if we want to use input or output statements in C++ program.

Line 3:   This is another header file “conio.h” which stands for console input output. This header file is required to use getch() function being used in line 8.

Line 4:   Every C++ program must have a main function to start execution. If main function is not present in a C++ program then the code will not be executed. There are many formats of main function but we usually use void main(). A pair of parenthesis is required after main word. It can be empty or may have some parameters as well. The keyword “void” has its own significance. Currently it means that main function is not returning any value to the operating system. We will discuss the different formats of functions later in this tutorial.

Line 5:   Every function including main function must start with curly bracket. We can say that body of the function is written between the pair of curly brackets {}.

Line 6:   std::cout is the statement which stands for standard character output. It displays the output on the standard output device which is usually screen. The output to be displayed can be string, variable or expression. In the current example it is string “Welcome”. Strings are always enclosed within double quotes. std::cout is always followed by insertion operator (<<). std::endl is used to change the line i.e. to change the line after displaying “Welcome” such that next output will be displayed on next line.

Line 7:   We can use as many std::cout statements as required. Kindly note that std:: appears before cout every time in the code example. “std” is a predefine namespace in C++. Instead of writing every time before cout, we can write it once before main function as below.

using namespace std;

After this, there is no need to write std:: before every cout. We can use cout as below.


cout<<“To the worderful world of C++”<<endl;

Line 8:   This is an optional function usually written at the end of C++ program. If it is not written then in some IDEs you will not be able to see the output. The output window closes as soon as the output is displayed and you will not have enough time to see the output. If you want to pause the output window then you can use the getch() or getche() function as the last statement of C++ program. It will display the output window until a character is entered or a key is pressed.

Line 9:   main function must be closed. The body of main function was opened in line 5 with left curly bracket. In line 9, the corresponding right curly bracket is used to close the body of main function. Anything written after this bracket is not part of the main function.

Kindly note lines 6, 7 and 8. These lines end with semicolons. This is a rule of C++ that every statement must be terminated with a semicolon.

More Related Articles For You

    C++ Tutorial

    C++ Quizzes