Hello world code in C++
- #include<iostream> //pre-processor
- using namespace std;
- int main(){
- cout<<"Hello World!"<<endl;
- return 0; //return type
- }
1. Comments
Two forward slash(//) signs are used to add comments in a program. It does not have any effect on the behaviour or outcome of the program. It is used to give description of the program you’re writing. For multi-line comments we can use (/*..*/) .
2. #include<iostream>
#include is the pre-processor directive that is used to include files in our program. Here we are including the iostream standard file which is necessary for the declarations of standard input/output library in C++.
3. Using namespace std
All elements of the standard C++ library are declared within namespace.Here we are using std namespace i.e "using namespace std".
4. int main()
The execution of any C++ program starts with the main function, hence it is necessary to have a main function in your program. ‘int’ is the return value of this function.
5. {}
The curly brackets are used to indicate the starting and ending point of any function and every opening bracket should have a corresponding closing bracket.
6. cout<<”Hello World!\n”;
This is a C++ statement. cout represents the standard output stream in C++. It is declared in the iostream standard file within the std namespace. The text between quotations will be printed on the screen. \n will not be printed, it is used to add line break. We can also use "endl" for line break. Each statement in C++ ends with a semicolon (;) .
7. return 0;
return signifies the end of a function. Here the function is main, so when we hit return 0, it exits the program. We are returning 0 because we mentioned the return type of main function as integer (int main). A zero indicates that everything went fine and a one indicates that something has gone wrong.
Instagram 👇
😊👍
ReplyDelete