January 13, 2021
Basic Syntax of C++ Program

C++ Basic Syntax

In this chapter, we would learn the basic syntax of the C++ programming language.

We would cover the following:

  1. Basic OOP Terms
  2. Structure of C++ Program
  3. Runing a C++ Program in Eclipse
  4. Code Blocks and Lines
  5. Identifiers in C++
  6. C++ Reserved Words
  7. Trigraphs
  8. Whitespe Characters in C++



1. Basic OOP Terms

OOP means Object Oriented Programming. Some OOP terms are given below:

Let’s reviews some of the important OOP terms.

Class – A template for creating new objects. A class specifies the attributes and behaviors of an object.

Class Variable – This is a variable that is shared by all objects that are created from the class. They are reference using the name of the class

Data Member – Variables defined in the class. They are used to hold data associated with the class

Instance Variable – These is a variable that is defined in a class but belongs only to the particular object of the class.

Instance – An object created from a class

Instantiation – The process of creating an instance from a class



2. Structure of C++ Program

Let’s write a simple Hello World program and then try to understand it. This is given below:


#include <iostream>
using namespace std;

//Program execution begins here
int main() {
   cout << "Welcome to C++"; // displays Welcome to C++
   return 0;


Let’s now see what the various componets are:

The first line #include <iostream> is known as preprocessor directive. It indicates that the file iostream is needed to compile this file

The second line using namespace std tell the compiler to use the namespace std. For now, think of a namespace as a collection of useful objects.

The line that begins in // is a comment. It is ignored by the compiler

The line int main() defines a function main. This is where the program execution actually starts

cout <<“Welcome to C++”, print the text to the screen

The line return 0 terminates teh main function



3. Runing a C++ Program in Eclipse

If you have correctly installled Eclipse and Set up the MinGW compiler as outlined in Lesson 2, then open Eclipse, click on file and choose C++ project.

I recommend you watch the video to get clarification.



4. Code Blocks and Lines

Notice that statements in C++ are terminated by a semicolon (;). It indicates the end of the line. For example:

a = 10;
b = b + 1;
sum(a, b);


Also, a block of statements are enclosed in curly braces. For example:

int main() {
   cout << "Welcome to C++"; // displays Welcome to C++
   return 0;



5. Identifiers in C++

As a programmer, you have the power to choose names of identifiers. Identifies are simply names given to variables.

However, there are some rule to follow when choosing names of identifiers.

  • Identifier names must start with letters A to Z or a to z or an underscore(_)
  • Special characters like %, !, * and others cannot be used in identifier names
  • Identifiers can contain numbers (but not as the first character)
  • Names in C++ are case-sensitive

Examples to valid identifiers are: eze, book1, _score, one2four, x, z, a1

Examples of invalid identifiers are: 45house, love.me,  [email protected], 1996



6. C++ Reserved Words

Reserved words are also known as keywords. They are words reserved for use by the language. Therefore you can’t choose them as identifiers. They are listed below:




7. Trigraphs

Though you may not use trigraphs often, it is a concept you should know. It is also called a trigraph sequence.

It is made up of three characters. These three characters represents a single character equivalent. It always begins with as question mark.

If you include a trigraph in a string, then when the program executes, it is replaced by the single-character equivalent.

Some trigraphs are given below:

TrigraphSingle-character equivalent

However, trigraphs are not supported by all compilers. So it is not generally used by programmers.



8. Whitespace Characters in C+++

Whitespaces are ignored by the C++ compiler. This could also be a blank line, newline and tab.

However, you should use whitespaces to seperate parts of your code. This makes your code more-readable.

Let’s take an example


Code written without whitespaces

// not very good code 
a = 10; b = b + 1; sum(a, b);
if(a == b) { c = e + ; }


Code written with whitepaces

// not very good code 
a = 10; 
b = b + 1; 
sum(a, b);

if(a == b) {
    c = e + ; 


Leave a Reply

Your email address will not be published. Required fields are marked *