C++ Syntax

Aarav Iyer
2 min readMar 28, 2023

--

File pic of a C++ code.

C++ has quite a simple syntax. I am going to take a simple sample program and explain some of the terms in it.

//Sample program
#include <iostream>
using namespace std;
int (main)
{
cout>>"Hello World";
return 0;
}

Line 1-‘//Sample program’ — Any line starting with // is a comment and will be ignored by the compiler.

Line 2- ‘#include <iostream>’ — This is the starting line of every program in C++ and is always present in a program. It is a library that has the basic functions of C++.

Line 3- ‘using namespace std;’ — This line says that we are using the names of objects from the standard C++ library. It is present in almost all programs unless it has a special output. An alternative to this line is adding ‘std::’ before every line.

Line 4- ‘int (main)’ — This is a function and all lines under it will be executed.

Line 5- ‘{’ — Curly brackets indicate the start and end of blocks of code.

Line 6- ‘cout>>“Hello World!”;’ — This is a line of code. ‘cout’ is used to display the text. Every line of code in C++ ends with a semi-colon (;).

Line 7- ‘return 0;’ — This basically states that the particular code block has come to an end. It is not necessary to add this line and the code will be executed it without it too.

Line 8- ‘}’ — This signifies the end of a code block.

Follow me to learn more about C++. You can find my blog about coding here.

Regards,

Aarav Iyer

References:

(1) https://www.w3schools.com/cpp/cpp_syntax.asp

--

--

Aarav Iyer
Aarav Iyer

Written by Aarav Iyer

A tech enthusiast learning Python and interested in anything related to space and the Universe.

No responses yet