Skip to main content

1.2) Your First C++ Program


(Note: line numbers below are not part of the program)

1   #include <iostream>
2
3   int main()
4   {
5     std::cout << "Hello World!" << std::endl;
6
7     return 0;
8   }

Explanation

  • Line 1 includes the iostream header, importing the C++ definitions that are needed to do input and output operations
  • Lines 3-8 defines a function called main, within which program execution begins; C++ code cannot run as a program unless a main function is defined
  • Line 7 specifies that the main function returns an integer value of 0 to its caller
  • Line 5 outputs the message Hello World! to the console, terminating it with a newline so that subsequent output appears on the following line