Wednesday, December 28, 2016

General C++ Program Structure

General C++ Program Structure

#include<iostream>
using namespace std;
int main(){
cout<<”hello world”<<endl;
return 0;
}


1. #include<iostream>
This line is a pre-processing directive. All pre-processing directives within C++ source code begin with a # symbol. This one directs the pre-processor to add some predefined source code to our existing source code before the compiler begins to process it. This process is done automatically and is invisible to us.

2. Using namespace std
The two items our program needs to display a message on the screen, cout and endl, have longer names: std::cout and std::endl. This Using namespace std directive allows us to omit the std::prefix and use their shorter names. name std stands for “standard,” and the Using namespace std line indicates that some of the names we use in our program are part of the so-called “standard namespace.”

3. Int main()
This specifies the real beginning of our program. Here we are declaring a function named main.

Q. Why always main? Is it possible to use any other name besides main?

Ans: Compiler has instruction to search for keyword main. If compiler is instructed to search for other name as your choice besides main then the program must have you search name which you instruct to search for compiler. Return value 0 is also not necessary, you can return any integer value. But compiler does not care about any value you return. So it is considered better to return 0.
4. cout<<”hello world”<<endl;

The body of our main function contains only one statement. This statement directs the executing program to print the message hello world on the screen. A statement is the fundamental unit of execution in a C++ program. Functions contain statements that the compiler translates into executable machine language instructions. All statements in C++ end with a semicolon (;).

No comments:

Post a Comment