Structure of a Program (Basic of C++ Hello World Program Example)
Structure of a Program (Basic of C++)
In this guide we will write and understand the first program in C++ programming. We are writing a simple C++ program that prints “Hello World!” message. Basically, best way to learn any programming languages by practicing. That's why we are writing program from a Basic level.
Hello World Program in C++
#include<iostream> // Header file for input output stream
using namespace std; // overcome from writing again and again std::
//This is where the execution of program begins
int main(){
//Print TExt on Screen
cout<<"Hello World\n";
return 0;
}
using namespace std; // overcome from writing again and again std::
//This is where the execution of program begins
int main(){
//Print TExt on Screen
cout<<"Hello World\n";
return 0;
}
Output:
Hello World
Let's discuss every part of the program
- #include<iostream>
2. using namespace std;
A namespace is designed to overcome this difficulty and is used as additional information to differentiate similar functions, classes, variables etc. with the same name available in different libraries. Using namespace, you can define the context in which names are defined. In essence, a namespace defines a scope.
3. int main()
'int main' means that our function needs to return some integer at the end of the execution and we do so by returning 0 at the end of the program. 0 is the standard for the “successful execution of the program”
4. cout<<"";
" cout " means "the standard character output device", and the verb " << " means "output the object" — in other words, the command " cout " means "send to the standard output stream," (in this case we assume the default, the console). Its belongs to iostream header file.
5. return statement
A C++ function can return a reference in a similar way as it returns a pointer. When returning a reference, be careful that the object being referred to does not go out of scope. So it is not legal to return a reference to local var.
return 0 implies that the program ran without an error and succesfully exited. return 1 implies that the program had an error and did not successfully run usually return 1 is used to denote that the program did not run as expected.
Post a Comment