C++ Basic Syntax

    Here in this tutorial, we will discuss the basic terminology that we use in C++. We will also define the basic structure we use when we create a C++ program Note: All the programs we build in this tutorial series are compiled and executed using Dev C++ IDE we suggest you to download Dev C++ if you want to execute the code or else you can execute the same program using any text editor and compiler.

    Basic C++ Terminology

    • Variable
    • Objects
    • Class
    • Methods


    Like in mathematics we use the variable to define any value, here in C++ we use the same concept, a variable is used to define and refer to value and there are various rules in C++ to define a variable. For example:

    int x = 2;

    here x is the variable , 2 is the value, and int is the data type which means integer.


    Object defines the state and behavior of any variable. The term object use often when we discuss object-oriented programming.


    A class is a blueprint of an object which holds all the properties and methods related to the object.


    A method defines the functionality related to an object. Note: Do not worry if you did not get all these terminologies, Objects, Class and Methods are the part of object-oriented programming and we defined each one in detail with suitable examples in the Object-Oriented programming section.

    C++ Basic Structure

    Let’s learn the basic C++ structure by creating a Hello World Program:

    #include <iostream>
    using namespace std;
    int main()
       cout << "Hello World";
       return 0;


    Hello World

    Behind the Code:

    Let’s break through the structure of the above program.

    • Line 1 st contain statement #include<iostream>, it is a header file which contains information about the program, keep in mind and learn this statement because we will use this statement in every program.
    • The second statement is using namespace std; this statement is the recent addition in the C++ compiler. It tells the compiler to use the std namespace, like #include<iostream> we use this statement in every program.
    • In the 3 rd line, we have a statement int main() followed by open curly bracket { here int mean integer, and main() mean the main function. In C++ if a statement has parenthesis () , then it would consider as a function. The int signifies that the return type of this main() function is an integer value.
    • When the compiler starts to compile a program , it first looks for the main() function, this means the main function compiled and execute first.
    • In the fourth line, we have the statement cout<< Hello World ”; the cout<< statement is used to print any string and the value of a variable.
    • And the last statement returns 0; this means the main function is returning an integer value. This statement is optional the above program will work fine if we do not add this statement.
    • The statements cout<<”Hello world ”; and return 0; are inside the part of main() function that’s why we used the curly brackets { } to enclose them.

    Once you have written the above program in your Dev C++ IDE save the program with ctrl+s , compile it by pressing f9 and if there are no errors execute it by pressing f10.

    Semicolons and Blocks in C++;

    Semicolons :

    In C++ we use Semicolons (;) to terminate any statement, basically semicolons are used to terminate the single line and individual statements. So whenever you write a statement in C++ make sure if the individual statement must end with a semicolon or else the compiler will throw an error. Example:

    x = 23;

    Here, after each statement, we have used the semicolon which signifies that the statement terminates there itself. We do not use a semicolon with functions that’s why you can see in the previous hello world program example with the main() function we did not pass any semicolon.


    If there is more than one statement inside a curly bracket we refer it a block. Example:

    int main()
       cout << "Hello World";
       return 0;

    Here cout << "Hello World"; and return 0; reside within {} which are the part of main() function. If we create a block using { opening curly brackets make sure that you close that block using closing curly bracket}.

    C++ Identifiers

    Identifies are the user-defined name given to value and object so we could access that object or value at any point inside a block. There are some rules to define an identifier:

    • The identifier name should start with any letter A to Z or a to z or Underscore (_)
    • We cannot use any digit at the starting of any identifier.
    • We cannot use any special symbol except underscore (_) to define a variable name
    • An identifier could be of any length.
    • Identifiers are case sensitive, so man and Man would be considered as two different identifiers.

    Example: Some valid identifiers.

    Man, mAN, mAn, _an, m_1, m___, ___n, etc.

    Some invalid Identifiers:

    2man , %man

    C++ Keywords:

    Keywords are the special names that are reserved by the program itself we cannot use them as identifiers. Some C++ keywords:

    asm else new this
    auto enum operator throw
    bool explicit private true
    break export protected try
    case extern public typedef
    catch false register typeid
    char float reinterpret_cast typename
    class for return union
    const friend short unsigned
    const_cast goto signed using
    continue if sizeof virtual
    default inline static void
    delete int static_cast volatile
    do long struct wchar_t
    double mutable switch while
    dynamic_cast namespace template


    For C++ white space could be a new line character, a blank space, tabs, and comments, C++ compiler ignore these white space. Example:

    int score;

    here we had provided a white space between int and score so the compiler could tell int is a datatype and score is the identifier. If we did not put the white space between int and score the compiler would read the statement as intscore and consider it as an identifier and throw an error “intscore is not defined”.


    This tutorial was all about the overview of those topics which we will cover in upcoming tutorials in detail.

    People are also reading: