Hello world Program in C++
Notes:
Hello world Program in C++ Programming Language:
#include <iostream>
#
Any statement that begins with # symbol, indicates it is a preprocessor statement. It must be handled by preprocessor.
#include
#include is one of the preprocessor directive. There are many other preprocessor directives available in C++. Ex: #include, #define, #ifdef, #ifndef, #endif, #error etc.
It tells to the preprocessor, to locate the given header file (i.e. iostream) within the CPP library, if it is available then include it inside cpp file (i.e. main.cpp).
< > : (pair of angle brackets)
- indicate locate the given header file inside the CPP library.
iostream : ( input output stream)
To use cout object, cin object etc. inside our CPP program we must include the iostream file in the beginning of the source code; because they are declared inside iostream file.
using namespace std;
There are 2 reasons for writing using namespace std;
1. The most commonly used cout object, cin object, endl object etc. are wrapped inside std namespace. 2. To use cout object, cin object, endl object etc. inside our program we must prefix them with std::. To use cout object, cin object ,endl object etc. without prefixing std:: then we must write the statement using namespace std; before the main() function.
int main()
{
return 0;
}
main() is a function. As the name itself indicating, it is the main function for every CPP application. Execution of any CPP program or an application begins at main() function.
int is a return type of main() function which indicates the type of value the main() function is returning to the OS.
return 0; indicates the program has got executed successfully without any errors. If any errors found then the compiler itself sends a nonzero integer value to the OS indicating there is any error in the program.
{} : (pair of flower brackets)
Pair of flower brackets indicates the block of code; used to group one of more statements. The opening flower bracket indicates the beginning of main() function and the closing flower bracket indicates the end of main() function.
cout << "Hello world" << endl;
cout (console output)
cout is the built in object; linked to the standard output (i.e. console window). It displays the given value inside the console window.
<< : (pair of less than symbols)
<< is the insertion or put to operator. It inserts or puts the content on the RHS to the object on the LHS
endl : (end line)
It moves the cursor to the Nextline so that subsequent output begins on the new fresh line.
Semicolon (;)
It indicates end of the instruction or statement. Every CPP instruction or statement must end with semicolon.
Example Code:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
return 0;
}