< C++ Tutorial

C++ 1st program compile and run

← Previous Next →



In order to run your C++ program, you need to compile it.
Compiler is the one, which converts source code to Machine code. In simple terms you can understand that a compiler converts human readable code to a machine readable format.

#include < iostream.h>
int main()
{
    cout<<"Hello World";
    return 0;
}

Steps to Run 

  • Write above code in a file and save the file with .cpp extension example - helloworld.cpp
  • Compile this code with any C++ compiler and Run .
  • It will display "Hello World" in the output screen.

 Understand the above program :

#include < iostream.h> 
The first line of the program #include is a preprocessor command, which tells a C++ compiler to include iostream.h file before going to actual compilation.
int main()
The next line int main() is the main function where the program execution begins. Every full C++ program begins inside a function called main . A function is simply a block of code that do something.
The main function is always called when the program first executes.
From main, we can call other functions whether they be written by us or by others or use built-in language features.
cout<<"Hello World";
cout is the standard output stream, it simply displays what is written within double quotes ("") or value of some variable (will be discussed later).
return 0;
The next line return 0; terminates the main() function and returns the value 0.

 

 Note: You can use online compilers like CPP Shell, IDEOne, CodePad, etc

In online compilers, there are some minor changes to be made:

  • "iostream.h" needs to be replaced by "iostream"
  • "using namespace std" needs to be added, it defines that all I/O are on standard I/O stream

The above code would change to:

#include <iostream>
using namespace std;
int main()
{
    cout<<"Hello World";
    return 0;
}
← Previous Next →