C++ - SPLessons

C++ Syntax

Chapter 3

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

C++ Syntax

C++ Syntax

shape Introduction

C++ is simple when compared to C and can be easily learnt. The basic CPP Syntax has to be known to learn C++ easily.

shape CPP Syntax

Below is the basic syntax for C++ programs.
#include<header_file>

namespaces std;
main()
{
// statements;
}

Header files

shape Description

Header Files are same in both C and CPP Syntax but there are no extensions like .h which reduces the difficulty for the user by providing defined libraries. In the below program,<iostream> represents input output stream.

#include<iostream>

Identifiers

shape Description

Identifiers are user-defined name of a program element. It uses “Class, Variables and Objects” that are declared using some statements. Standard namespaces in a program can be written in two ways such as:

  • Using namespace std;
  • Use the namespace in front of cout by placing Scope Resolution Operator ::.

Eg: std::cout<<“splessons”;

To learn more about namespaces, refer Namespaces in C++

Functions

shape Description

Curly Braces

Any C/C++ code block is delimited by curly braces{}. The bodies of a function,loop,structure, and class, everything must be enclosed between curly braces only with very few exceptions.

They are set of instructions that has main section similar to the C program which is of integer type and is enclosed within { }. The execution starts from this point. This part of the program is called as Function body.

Comments

Comments are the statements used in the C++ code to make the user to understand what is happening in the source code given to the compiler without reading the actual code. Comments makes the new user to learn the language easily. Comments are of two types.

Single line comments helps in commenting shortly at the end of a code and can be represented with //.

cout<<"splessons"<<endl;   // cout is console output

Multi line comments can be used when there is lot of information to be inserted in source code for ease of programming.Multi-line comments can be even in 1 or more lines. They can be represented with /*...*/ .

/*Welcome to SPLessons
You are reading C++-programming tutorial
This program is about summing of two numbers*/

Statements

Statements are the instructions of programs and are considered as the building blocks of computer programs. C++ statements can be of many types following certain rules and these can be discussed in further chapters.

In the above program, cout is derived from iostream which is used to display the output on the console window. ‘cout’ is then followed by stream-insertion operator <<.

The output statement enclosed in double quotes " " which has to be printed. Every C++ statement should end with a semicolon ;.

cout<<"Welcome to SPLESSONS\n"; //C++ statement

Whitespaces

Whitespaces and empty lines are basically ignored by C++. So, they can be inserted.

New Line

A new line can be inserted in two ways. By using the well-known escape sequence \n, or using the endl manipulator.

shape Example

#include<iostream>    //header file
#include<cstdlib>
using namespace std;  //identifiers
int main()            //functions
{ // curly braces
	cout << "Hello World!!!" << endl; //C++ statement
	cout << "Welcome to SPLESSONS"; // C++ statement
	return 0;
}

Output:

Hello World!!!
Welcome to SPLESSONS

The above program has “Header files, Identifiers and Functions” which gives the basic syntax of a C++ program.

Summary

shape Key Points

“CPP Syntax” draws out the following important points.

  • iostream is the header file in C++
  • “using namespace std” helps to include the identifiers
  • Scope resolution factor can be represented by ::

shape Programming
Tips

  • Know the difference between <<  and  <.
  • Every C++ statement must end with a semicolon( ; )
  • Without header file, program cannot be executed and this must be placed at the top of the program.
  • Do not give space between # and include.