C++ Syntax – Strengthen your roots to beautify the branches

Free C++ course with real-time projects Start Now!!

The term “Syntax” is a set of pre-defined protocols or rules that we need to follow in a programming language. Every programming language has its own unique syntax. Similarly, C++  Syntax also has its significance.

At this stage, we would not take any difficult programs into consideration. We would start off with the most popular program for beginners called the “Hello world!” program. At the end of this tutorial, you would be pretty familiar with the basic structure of a C++ program.

Here, we are going to discuss not only basic C++ syntax but also components of the C++ program. So, get ready to make your roots strong.

When the roots are deep, there is no reason to fear the wind.

1. Basic C++ Syntax

The expression “Syntax” refers to the rules that we need to strictly follow while writing a program. In order to get an error-free output, we diligently need to follow these predefined rules.

In order to develop a clear and concise understanding of the C++ syntax, let us take the “Hello world!” program into consideration. This is the simplest program you can make in C++.

Before we move towards example, install C++ (g++ compiler) on Linux

Here is a C++ program to print “Hello world!” on the output screen:

#include<iostream>
using namespace std;

/* This is my first C++ program */

int main()
{
cout<<"Hello world!"<<endl;
return 0;
}

Code-

C++ Hello World Example

Output-

Output of helloworld C++

From the above program, you might have got a basic idea about the syntax and structure of a C++ program, how the elements of the program are placed together and how we get the output on the display screen.

2. Components of the C++ Program

From the above program, it is pretty clear that there are various components in a C++ program which includes header files, the main function and the body of the program.

Now, let us discuss each and every component of the C++ program in detail.

Components of the C++ Program

2.1  Header Files

In the above program, we have used one header file.

#include<iostream>

In this header file, ‘iostream’ stands for the input-output stream.
In general, a header file instructs the C++ compiler to include all the functions associated with that header file.

Here, the <iostream> header file allows you to use the input and output operations. We have performed an output operation of displaying the message “Hello world!” on the screen with the help of the cout function.

2.2 Namespace

Namespace refers to the declarative region that gives scope to all the identifiers (a unique name given to variables and functions according to a set of rules) used inside it.

In the “Hello world!” program, we have written a statement:

using namespace std;

The above statement is written in every C++ program. It is a relatively new concept introduced by the latest C standards.

This statement basically instructs the C++ compiler to use the standard namespace.
Another way of using namespace is by the use of the scope resolution operator “::”

This is how we will use it:

std::cout<<“Hello world!”;

It instructs the compiler that every time you write cout, you are referring to std::cout

Hello World example of C++

2.3 The Main Function

Every C++ program necessarily contains at least one function, that is, the main function.
Every statement in C++ executes line by line in the main function.

According to the latest C++ Standards, it is necessary to specify the return type of the main function as int.

Explore more details  Functions in C++ 

2.4 {AND}

The curly braces ‘{’ and ‘}’ denote the opening and closing of a statement respectively.
In the “Hello world!” program, the opening and closing braces denote the start and end of the main function.

2.5 Print Statement

In order to print or display a message on the screen, use the ‘cout’ function. The ‘cout’ function is followed by the output operator <<’ followed by the text to be printed enclosed within double quotes.

It is similar to the printf() function in C except for the fact we do not require format specifiers.

cout<<“Hello world!”<<endl;

Here, the endl statement stands for the end of the line. It instructs the compiler to move to the new line. It is similar to the ‘\n’ escape sequence.

2.6 return 0;

The return 0 statement signifies that the main function would return a null value.

2.7 Tokens in C++

Term ‘token’ refers to the smallest possible individual unit in a program.‘There are a total of 5 types of tokens in C++. It is absolutely similar to the C programming language.

They are:

  • Keywords
  • Identifiers
  • Constants or Literals
  • Punctuators
  • Operators

For a detailed discussion, check out the Tokens in the C++

2.8 Comments in C++

Comments in C++ are statements that are not executable when the program is run. They are simply used for the user’s convenience.

There are 2 types of comments in C++:

  • Single line comments: Anything written b=after ‘//’ operator is considered as a single-line comment

For instance,

// Welcome to DataFlair tutorials!

  • Multi-line comments: Anything written within ‘/*’ and ‘*/’ is considered as a multi-line comment

For instance,

/* Welcome to DataFlair tutorials!
We Hope you enjoy our tutorials! */

In the “Hello World” program, We have used a multi-line comment

/* This is my first C++ program */

2.9 Whitespaces in C++

White spaces are nothing but blank spaces. Whitespaces separate elements of a program from one another.

For instance,
In the statement

int value;

If you don’t separate int and value with whitespace then the statement would make no sense and you would get a compilation error.

2.10 Semicolons in  C++

We use semicolons in C++ to indicate the termination of a statement. If they are not used as and when required, you would get a compilation error.

3. Basic Rules for Writing C++ Program

C++ is a case sensitive language. Therefore, uppercase and lowercase characters are treated differently. It must be kept in mind while coding in C++. Most of the keywords in C++ are in lowercase.

All statements must necessarily terminate with a semicolon.

Whitespace should be provided duly between keywords and variables in C/C++.

4. Quiz on C++ Syntax

5. Summary

Now, you are familiar with the basic syntax of C++ and can make any program with the help of components we discussed above. Every programming language can be differentiated on the basis of their syntax. Syntax plays a vital role everywhere. The best way to learn and master anything is practicing.

Practice makes coding perfect.

If you’re still having some confusion then let us know in the comment section. Our experts will help you in the best possible way.

Did you like this article? If Yes, please give DataFlair 5 Stars on Google

follow dataflair on YouTube

2 Responses

  1. Mo. Shams says:

    My computer has Vesta operating system; it is about 13 years old. I could use some help with installing “Compiler” to start writing program.
    install C++ (g++ compiler) on Linux, this is for Linux; could it good for Vesta?
    also; an unrelated request: Could use help to upgrade Vesta to Window 10

  2. Rick Van Vorhis says:

    What is a “Solution” and how is it used. How are test C++ Programs stored in a “Folder,Directory”. Can I Create 100 Test Programs and put them in the Same Directory?
    Where does the “Project” come in the Picture.

Leave a Reply

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