Simple C++ program :-
As the syntax of simple C++ program has already described in the previous topic of syntax. But, as it was also told the word syntax will be repeated again and again. So, lets have an overview that what is syntax? So it can be explained as the way of writing down a program by keeping all rules and regulations in front of you which are compulsory for the proper execution of a program, like adding header files, specifying regions, and proper standard library.
So here a simple program is shown below:
#include <iostream>
using namespace std;
int main ( )
{
cout<<"Welcome";
return 0;
}
#include <iostream> :-
In the above program the first line is [ #include <iostream> ]
in it the word #include is syntax of writing down a C++ program and as it is already explained before that syntax is compulsory with proper rules and regulations so it is also compulsory.'
Now lets move to the next part that is <iostream> so it is important to be addicted that it is Header file.
Now the question is what is header file ? The question can be answered as it is the file which includes all the functions which are to be performed and we can also make the idea clear for the instant by saying that it includes all the inputs (which has to be taken from the programmer) and outputs (which has to be displayed on the screen after the execution). And it is present in the standard library.
Using namespace std :-
Now, lets move to the next line that is [ using namespace std; ].This
line tells the compiler to use a group of functions that are part of the
standard library (std).
In it using is a keyword and also the syntax of this line which shows the usage of the namespace and std.
Lets discuss what is namespace ? So, it can be answered as
1). A
namespace is a declarative region that provides a scope to the identifiers (the
names of types, functions, variables, etc.) inside it.
2). Namespaces
are used to organize code into logical groups and to prevent name collisions
that can occur especially when your code base includes multiple libraries.
3). All
identifiers at namespace scope are visible to one another without
qualification.
Overall the basic concept is, it provides caliber or boundary or region to the compiler by which it prevents to get confused between two things of the same category. For example if we perform a task of volume gain then compiler will be confused because the function, gain in volume is performed in music library and in physics library also. If we want our program to make a sensible execution then it is compulsory to use namespace because in this way compiler will know which task to perform.
Now the term which was left is std, so about it now the concept must be restricted to just, it is short form of standard and we can also say it is standard library which includes all the header files and the header file <iostream> which we discussed above is one of it's header files.
int main ( ) :-
For this part just the only concept which we should have to keep in our mind is it is the starting point of a program. Means program starts from this point and all the lines written before are the syntaxes.
cout<<"Welcome"; :-
cout is used to print the lines or words which we have to print. And it's proper way is to write the line or word in inverted commas as shown in above written program with proper logics.
return 0; :-
After writing a proper program at the end
return 0 is written because if we do not write it then whenever we give any input to the program then after the execution in spite of closing the display the program will not be stopped but keep on running which causes a burden on the RAM so that's why return 0 is used because it tells the compiler that the output has displayed and 0 output is left so automatically program will be stopped and any kind of loss will not be occurred or not even expected.
Blocks :-
The logically connected statements are called blocks. And these lines are written in curly braces { } . The logics are << , "" etc...
Semicolons :-
Semicolons (
;) are the terminators which are used to end the statements because when any statement ends then it is just visible to us not to the compiler so in order to make this scenario clear to the compiler we use terminators or also can say semicolons.