First C++ Program Hello World

Here is a C++ program to print Hello World on screen. Let's start with a simple C++ program to print "Hello World" string on screen. It become the traditional first program that many people write while learning a new programming language. This program is very useful for beginners to understanding basic syntax of C++ programming language.


C++ Program to Print Hello World

// C++ program to pring hello world string on screen
#include <iostream>
using namespace std;

int main() {
    cout << "Hello World";
    return 0;
}
Output
Hello World

In above program, we are just print "Hello World" message on screen. Although, a very simple C++ program but very useful in understanding the basic structure of a C++ program. Let us look various parts of the above program line by line:

  • // C++ program to pring hello world string on screen
    This line is a single-line comment of C++ language. Single-line comments begin with // till end of the line. Everything on the line after // is ignored by compiler.
  • #include
    #include is a preprocessor directive to includes the header file in our 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.
  • int main() {
    Every C++ program must have only one main() function where program execution starts. The int is what is called the return value of main function.
  • cout<<"Hello World";
    The cout is the standard output stream which prints the "Hello, World!" string on the monitor.
  • return 0;
    It is the Exit status of the program. Returning 0 means informing operating system that program successfully completed, whereas returning 1 means error while running program.

C++ Program to Print Hello World Multiple Times

In this program, we will print "Hello World" string multiple times on screen using a for loop. Don't think much about for loop now, we will discuss about it later.

// C++ program to pring hello world string 10 times
#include <iostream>
using namespace std;

int main() {
    int i;
    for(i = 0; i < 10; i++){
        cout << "Hello World" << endl; 
    }
    return 0;
}
Output
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World

Points to Remember
  • Every C++ program must have one main() function where program execution starts.
  • A single-line comments begin with // till end of the line. Comments are ignored by C++ compilers.
  • Every statement of a C++ program must end with a semicolon otherwise compiler will report syntax error while compiling.
  • Some header files must be included at the beginning of your C++ program.
  • Main returns an exit status to Operating system informing about it's termination state. Whether program executed successfully or an error has occurred.

Recommended Posts
C++ Taking Input From User
C++ Program for Input and Output of Integer
C++ Program to Perform Addition Subtraction Multiplication Division
C++ Program to Check Whether a Character is Vowel or Not
C++ Program to Check Whether a Character is Alphabet or Not
C++ Program to Multiply two Numbers
C++ Program to Find Sum of Natural Numbers
C++ Program to Add Two Numbers
C++ Program to Read an Integer and Character and Print on Screen
C++ Program to Swap Two Numbers
C++ Program to Count Zeros, Positive and Negative Numbers
All C++ Programs