Site icon DataFlair

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-

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

Output-

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.

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

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:

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++:

For instance,

// Welcome to DataFlair tutorials!

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

Time limit: 0

Quiz Summary

0 of 15 Questions completed

Questions:

Information

You have already completed the quiz before. Hence you can not start it again.

Quiz is loading…

You must sign in or sign up to start the quiz.

You must first complete the following:

Results

Quiz complete. Results are being recorded.

Results

0 of 15 Questions answered correctly

Your time:

Time has elapsed

You have reached 0 of 0 point(s), (0)

Earned Point(s): 0 of 0, (0)
0 Essay(s) Pending (Possible Point(s): 0)

Categories

  1. Not categorized 0%
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  1. Current
  2. Review / Skip
  3. Answered
  4. Correct
  5. Incorrect
  1. Question 1 of 15
    1. Question

    #include <iostream>

     

    using namespace std;

     

    int main()

    {

    cout<<“Hello World”;

     

    return 0;

    }

    Correct
    Incorrect
  2. Question 2 of 15
    2. Question

    #include <iostream>

     

    using namespace std;

     

    int main()

    {

    cout< <“Hello World”;

     

    return 0;

    }

    Correct
    Incorrect
  3. Question 3 of 15
    3. Question

    using namespace std;

     

    int main()

    {

    cout< <“Hello World”;

     

    return 0;

    }

    Correct
    Incorrect
  4. Question 4 of 15
    4. Question

    #include <iostream>

     

    class temp{

    public:

    int x,y;

    int add(int x, int y)

    {

         return (x+y);

    }

    };

    int main()

    {

    temp t;

    cout<<t.add(5,6);

    return 0;

    }

    Correct
    Incorrect
  5. Question 5 of 15
    5. Question

    #include <iostream>

    using namespace std;

    class temp{

    public:

    int x,y;

    int add(int x, int y)

    {

         return (x+y);

    }

    };

    int main()

    {

    temp t;

    cout<<t.add(5,6);

    return 0;

    }

    Correct
    Incorrect
  6. Question 6 of 15
    6. Question

    #include <iostream>

    using namespace std;

    class temp{

    public:

    int x,y;

    int add(int x, int y)

    {

         return (x+y);

    }

    };

    int main()

    {

    temp t;

    t.x=9;

    cout<<t.x;

    return 0;

    }

    Correct
    Incorrect
  7. Question 7 of 15
    7. Question

    #include <iostream>

    using namespace std;

    class temp{

    public:

    int x,y;

    int add(int x, int y)

    {

         return (x+y);

    }

    };

    int main()

    {

    temp t;

    t.x=9;

    t.y=10;

    cout<<t.add(t.x, t.y);

    return 0;

    }

    Correct
    Incorrect
  8. Question 8 of 15
    8. Question

    #include <iostream>

    using namespace std;

    class temp{

    public:

    int x,y;

    int add(int x, int y)

    {

         return (x+y);

    }

    };

    int main()

    {

    temp t1, t2;

    t1.x=9;

    t2.y=10;

    cout<<t1.add(t1.x, t2.y)

    return 0;

    }

    Correct
    Incorrect
  9. Question 9 of 15
    9. Question

    #include <iostream>

    using namespace std;

    class temp{

    public:

    int x,y;

    int add(int x, int y)

    {

         return (x+y);

    }

    };

    int main()

    {

    temp t1, t2;

    t1.x=9;

    t2.y=10;

    cout<<t1.add(t1.x, t2.y);

    return 0;

    }

     

    Correct
    Incorrect
  10. Question 10 of 15
    10. Question

    #include <iostream>

    using namespace std;

    class student{

    public:

    int rollNo, marks;

        

    };

    int main()

    {

    student s1, s2;

    s1.rollNo=9;

    s2.rollNo=10;

    s1. marks=80;

    s2.marks =s1.marks;

    cout<<s2.marks;

    return 0;

    }

    Correct
    Incorrect
  11. Question 11 of 15
    11. Question

    What among the following is cout?

    Correct
    Incorrect
  12. Question 12 of 15
    12. Question

    what library should be used if cout is used in a program?

    Correct
    Incorrect
  13. Question 13 of 15
    13. Question

    Which among the following lines of code is erroneous?

    Correct
    Incorrect
  14. Question 14 of 15
    14. Question

    which among the following is true for methods

    Correct
    Incorrect
  15. Question 15 of 15
    15. Question

    What are statement terminators?

    Correct
    Incorrect

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.

Exit mobile version