C++ Syntax

Parts of C++ Code

To understand the C++ the code, we are going to look at some part of a sample code. Let us take the following code-

//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 ’ — 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.


To know more about coding, follow this blog. You canfind my Medium account here.

Regards,

Aarav Iyer

References:

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

Aarav Iyer

I am a technology and programming enthusiast, currently a high school student. I love drawing and am quite interested in aeronautics and astrophysics too. My favourite pastimes are reading books, blogging and skywatching with my telescope.

Post a Comment

Next Post Previous Post