Thursday, 17 August 2017

Basic Syntax Of C++(what is main,#include,iostream,namespace and comments in c++)

program to print a string on the screen(DevC++)

The above program is one of the simplest programs that can be written in C++, but it does include basic elements that every C++ programs have. Let us have a  look at these elements one by one:

1. COMMENTS IN C++ PROGRAM 
Comments are pieces of code that the compiler simply does not execute.Comments are written for the explanation of the steps or program.There are two ways to insert comments in C++ programs.

(i) Single line comments with / /:-The comments that begin with //are single line comments.The compiler simply ignores everything following in that same line.In above picture line 1 represents the single line comment, whatever written after // will be discarded.
(ii)multi-line comments with /*.......*/:-The comments begin with /* and end with */.That means, everything that falls between /* and */will be discarded.In above picture line, 8 and 9 represent multi-line comments.

2. #INCLUDE
The statements that begin with the #(hash) sign are directives for the preprocessor(a computer program that modifies data to conform to the input requirements of another program).That means these statements are processed before compilation takes place.INCLUDE statement tells compiler's preprocessor to include the header file in the program.

3. INT MAIN (){.....}
The line indicates the beginning of the main function.Whatever written between the curly brackets {}
will be executed.It is essential to have the main function.INT is data type will be explained further.

4. IOSTREAM(header file)
The header file iostream is included in every C++ program to implement input/output facilities, without this file we cannot input or take the output from the program.

5. COUT(pronounced "see-out")
Cout stands for console output.It helps in printing the values on the screen.

6. CIN(pronounced "see-in")
Cin stands console input.It takes input from input devices.

7. INSERTION OPERATOR("<<")
Insertion is performed by insertion operator"<<".

8.EXTRACTION OPERATOR(">>")
Extraction is performed by the extraction operator">>".

9. SEMICOLON(;)
The semicolon (;) character used to finish every executable statement of a C++ program and it must be included after every executable instruction.

10. RETURN 0
The return instruction makes the main() to finish and it returns a value, in this case, it is returning 0(zero).Returning 0 is the most usual way of telling that program has terminated normally.

                      click here to know what is namespace  

2 comments: