Table of Contents
Back Button
Back to Chapter
C++ Setting Up
C++ Introduction
No items found.
C++ Basics
No items found.
C++ Control Statements
No items found.
C++ Logical Operators
No items found.
C++ Procedural Programming
No items found.
C++ Structural Programming
No items found.
C++ Implementation of OOPS
No items found.
C++ Arrays and Vectors
No items found.
C++ Error Handling
No items found.
C++ File Handling
No items found.

C++ Syntax

The literal meaning of syntax is grammar. In order for us to understand the syntax of C++ better let us consider our first C++ "Hello World" program.

#include using namespace std; main() { cout<<"Hello World"; }

OK! lets break down the code in to 3 parts.

Part 1:

#include

This is a header file library that lets us work with input and output objects, such as cout used in line 4. As we go ahead we will learn many more header file libraries and we will also understand the importance of having libraries in a programming language.

Part 2:

using namespace std;

Using this allows us to use names for objects and variables from the standard library. Think of this as a necessary line of code in your programs. Explaining what namespace is at this stage will only confuse you more.

part 3:

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

This is where the interesting part occurs, main is the function name and the value it returns is of int type, and the "{" indicates the start of the function and "}" indicates the end of the function.

This is the basic syntax for writing any function in C++:-

return data-type function_Name (formal parameters) { ...............//lines of code }

Inside the main function, we notice that all lines end with a ";" the end of this mark of the line in C++. Each line of code must end with a ";"  every time the compiler encounters ";" it assumes that the line ends here and anything written after a semicolon is considered as a new line.

Formal parameters are those variables and their type as they appear in the early sample of the function or method.
return 0;

return is a keyword that is used to assign a value to a function. It can also be used to call the same function recursively.

cout<<"Hello World";

cout is short for the C standard output stream. It instructs the computer to display output on the output device which in most cases is the display screen.|<<| is the insertion operator, which is preprogrammed for all standard C++ data types, sends bytes to an output stream object.

Everything is written within (".....")will be treated as a string in C++. A string is a list of characters that ends with a null character '\0'.

// in C++ double slash indicates a line comment. This means everything written after the // is ignored by compiler till the end of the line. We can write multiple lines of comments using /....../ . The compiler ignores everything in between the slash and asterisk whether its multiline or single line.

Writing comments is a good programming habit, as comments usually explain what a line/ block of code is doing. Writing comments makes your code understandable to others and will work as a reminder for you in case you have to check your code after a really long time.

Ask queries
Contact Us on Whatsapp
Hi, How Can We Help You?