C++ Syntax

 

The following code is written in C++

We shall look at each element of the following code in detail to understand the syntax of C++

 


#include <iostream>
using namespace std;

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

 

#include <iostream>: This is a header file library. <iostream> stands for standard input-output stream. It allows us to include objects such as cin and cout, cerr etc.

 

using namespace std: Means that names for objects and variables can be used from the standard library. It is also used as additional information to differentiate similar functions. 

 

int main(): The function main is called just as in C. Any code inside its curly brackets {} will be executed.

 

cout: is an object used to print a particular text after << in quotes. In our example it will output "Hello World". (for personal reference we can say it is similar to printf in c)

 

return 0: Terminates the function main

 

Note:

1) Every C++ statement ends with a semicolon ';'

2) Compiler ignores white spaces. Multiple line spaces are used to make the code more readable.

 

Omitting Name spaces:

C++ programs run without the standard namespace library. This can be done by writing std keyword followed by :: operator inside int main()

Example:

#include <iostream>
int main()
{
    std::cout <<"Hello World";
    return 0;
}