C++ First Program Hello World

In this tutorial, we are going to learn about the basic structure of a C++ program. Let's first write a simple C++ program to print "Hello World" string on screen. This program is very useful to learn the basic syntax and semantics of the C++ language. It become the traditional first program that many people write while learning a new programming language.

C++ Program to print Hello World string

#include <iostream>
using namespace std;

int main() {
    // Printing "Hello World" string
    cout << "Hello World";
    return 0;
}

Let us look various parts of the above program line by line:

  • #include <iostream>
    #include is a preprocessor directive to includes the header file in our proram. C++ language contains various function library(header files) which contain information that is either necessary or useful to your program. In this program we are using one such header called iostream which is required for input and output.
  • using namespace std;
    This line tells the compiler to use the std namespace. Namespaces are used to avoid naming conflicts and to make it easier to reference operations included in that namespace(here we are using std namespace).
  • int main() {
    The next line is int main(){. Every C++ program must have a main() function, and every C++ program can only have one main() function where program execution begins. The int is what is called the return value. The two curly brackets {} are used to group all statements together.
  • // Printing "Hello World" string
    This line is a single-line comment of C++ language. Comments are written for better readability of code, this line is ignored by compiler. Single-line comments begin with // till end of the line. Everything on the line after // is ignored by compiler.
  • cout<<"Hello World";
    This line prints "Hello World" string on the screen. The cout is an object of standard output stream which is defined in iostream header. cout is used for displaying data on the screen.
  • return 0;
    This statement is returning 0 to calling function, in this case operating system. int main() declares that main must return an integer. Returning 0 means informing operating system that program successfully completed, whereas returning 1 means error while running program.
Points to Remember
  • C++ support both single line comment as well as multi line comments.
  • Every statement must end with a semicolon.
  • We must include iostream header for any input and output operation in our C++ program.
  • Every C++ program must have only one main() function where program execution starts.
  • Main returns an integer to Operating system informing about it's termination state. Whether program executed successfully or an error has occurred.

How to Compile and Execute your C++ Program

  1. Write
    • Open a text editor and type the above mentioned "Hello World" code.
    • Save this file as HelloWorld.cpp.

  2. Compile
    • Open command prompt and go to your current working directory where you saved your HelloWorld.cpp file.
    • Compile your code by typing "g++ HelloWorld.cpp" in command prompt. Your program will compile successfully, If your program doesn't contain any syntax error.
    • It will generate an a.out file.

  3. Execute
    • Now run your program by typing "a.out" in command prompt.

  4. Output
    • You will see "Hello World" printed on your console.