C++ basic syntax

05 min read

Structure of C++ Program

  • Every C++ program consists of one or more modules called functions. One of the functions must be the main function. Program execution always starts from the main function, which may later on call other functions.
  • C++ is case-sensitive. So, we need to follow the same case throughout.

 

cout<<"Hello World";

This statement will display 'Hello World'. 'cout' is a predefined object that represents the standard output stream in C++. Here, the standard output stream represents the screen. The operator << is called the insertion or put to operator. It inserts the contents on its right to the object on its left. Any information that should be displayed on the output screen should be enclosed in double coats.

 

int main()
{
cout<<"Hello World";
}

Every C++ statement must be enclosed within a function (for a simple program it is usually the main function).

 

include<iostream.h>
int main()
{
cout<<"Hello World";
}

The declaration for 'cout', '<<', etc are defined in the header file 'iostream.h'. Hence this header file must be included if these declarations are to be used in the program. The first line is a reference to this file. The inclusion of this required information will be handled automatically by the compiler.

 

include<iostream.h>
int main()
{
cout<<"Hello World";
return 0;
}

Here the word 'int' before 'main' denotes that the main( ) function will return an integer(decimal number) value alter its execution. So. 'return 0' statement at the end helps to return the integer zero. This should be the last statement in the program.

 

include<iostream.h>
int main()
{
cout<<"Hello World";
system("pause");
return 0;
}

The above-mentioned code will display the message but the output screen will vanish immediately. So, to retain the output screen as long as we want, system ("pause") statement can be used. 'system' is a function used to execute any operating  system command like 'pause' which means
to stop temporarily.

 

C++ keywords

Keywords are the words whose meaning has already been explained to the C compiler.

They have a specific meaning and they implement specific C++ language features.

Keywords can be used only for their intended purpose. They cannot be used as names for variables or other user-defined program elements.

There are a total of 48 keywords in C++.

 

C++ Identifiers

These are names given to various program elements such as variables, functions arrays, structures and classes. The rules for identifiers:

  • Only alphabetic characters, digits and underscores are permitted.
  • The names must begin with an alphabet or an underscore character. Cannot start with a digit.
  • Uppercase and lowercase letters are distinct.
  • A keyword cannot be used as an identifier name.
  • White space is not allowed.

 

Comments in C++

  • Comments are used for enhancing the readability and understandability of a program. These are not executable.
  • A comment may start anywhere in the line and whatever follows till the end of the line is simply ignored by the compiler. (It is only for the understanding of the programmers and not a required statement in the C++ Program)
  • Single line comment can be given in any of the following, 2 ways
    Ex: //This is an example of single line comment , /*This is an example of single line comment*/
  • Multi-line comment :  /*This is an example of multi-line comment*/
POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error