Hello World in C++
Create your first C++ 'Hello world' example, just write and learn this simple program to get started with the c++ programming language and spend some time to learn this example. and say hello to c++ programming.
_____________________________________________________________
#include <iostream>
int main()
{
std::cout << "Hello World!" << std::endl;
}
So let's examine each part of this code in detail:
- #include <iostream>
iostream is a standard library header file which contains the definitions of the standard input and output streams. And these definitions are included in the std namespace.
The standard input/output (I/O) streams provide ways for programs to get input from and output to an external system -- usually the terminal.
_____________________________________________________________
- int main() { ...
- defines a new function named main. By convention, the main function is called upon execution of the program. There must be only one main function in a C++ program, and it must always return a number of the int type.
And here is int main() called as a function return type.
- If no return statement is declared, then the program itself returns 0 by default as a return type, We don't need to explicitly return 0;
_____________________________________________________________
- std::cout << "Hello World!" << std::endl; prints "Hello World!" to the standard output stream:
- std is a namespace, and :: is the space resolution operator which allows look-ups for an object by name within a namespace.
There are many namespace.
- std::cout is the standard output stream object, defined in iostream, and it prints to the standard output (stdout).
For Example :
cout<<"This text will print on terminal"<<endl;
- This statement will print "This text will print on terminal" end controllers will go on next line because of end; statement.
_____________________________________________________________
- \n will be responsible for new character escape sequence for the newline characters.
For Example : cout<<:Hello Word\n";
_____________________________________________________________
Comments In C++
Comments are used to show programming instructions, Everything you put in comment will be ignored and compiler will not execute those written statement.
There are two types of comments in C++
Single line Comments
We use double forward-slash // for single-line comment until a newline comes
// declaring a variable
int age;
// initializing the variable 'a' with the value 2
age = 2;
We use /* for starting for comment and */ for ending the comment
/* declaring a variable
to store salary to employees
*/
int salary = 2000;
Why comments are important?
- It makes code easier to read and maintain.
- Expose the functionality of code
- Can show history of code or reasoning behind the code
- can give credits, notes and contributes directly in comments