First program
C++ program to display "Hello World"
#include <iostream>
using namespace std;
int main() { // Main() function: where the execution of program begins
cout<<"Hello World"; // prints hello world
return 0;
}
-
#include:
- all lines that start with pound (#) sign are called directives and are processed by a preprocessor which is a program invoked by the compiler.
- The #include directive tells the compiler to include a file.
- #include<iostream>. It tells the compiler to include the standard iostream file which contains declarations of all the standard input/output library functions.
-
using namespace std:
- used to import the entirety of the std namespace into the current namespace of the program.
- The std namespace is huge. So the statement using namespace std is generally considered a bad practice.
- The alternative to this statement is to specify the namespace to which the identifier belongs using the scope operator(::) each time we declare a type.
-
int main()
- used to declare a function named "main" which returns data of integer type.
- Execution of every C++ program begins with the main() function, no matter where the function is located in the program.
-
std::cout<<"Hello World";
- tells the compiler to display the message "Hello World" on the screen.
- A semi-colon ; is used to end a statement. Semi-colon character at the end of the statement is used to indicate that the statement is ending there.
- The std::cout is used to identify the standard character output device which is usually the desktop screen. Everything followed by the character << is displayed to the output device.
-
return 0;
- This statement is used to return a value from a function and indicates the finishing of a function.
- <iostream> must be included to use std::cin and std::cout.
- It is a good practice to use Indentation and comments in programs for easy understanding.
Comments
// this is a single line comment
/*
this is a
multi-line comment
*/