The most important and compulsory thing for a programmer is to learn basic syntax of programming languages. For this purpose, mostly programming institutes start to teach their students with a kind of standard first program which is called "Hello World". It is used to teach and learn how to create, open and run the programming file and most important how to display or print-out text on screen.
In this article we will explore the detailed explanation of First Program Hello World, source code with code & output and dry-run with example.
Table of Content
Explanation:
We will understand all the details of our topic "First Program Hello World in C++".
Creating New File:
The most important and basic thing in any programming language is to create New File. For this purpose follow below steps:
- First of all Openany installedIDE of C++on your computer. The IDE stands for integrated development environment, consist of Code Editor and Compiler or Interpreter of specific language. I will use Dev C++ an IDE for C and C++ to create and run files of these languages on it.
- Then click on New File button on Top Left corner of Dev C++, a new File will be created with title of Untitled File.
Basic Code:
After it, first we will enter Basic Code which is compulsory before writing our own main code. For that basic code, we will write followings:
- First import built-in standard library / header file iostream which we needed for basic code which is used to print out and user-input anything. But iostream standard library has many functions, classes, variables etc with same name so to avoid any ambiguity for compiler we also add using namespace std; command after iostream library. Remember you can import any built-in and custom libraries / header files.
#include <iostream> using namespace std;
- Then we will write main() function with empty body except a one command at end curly bracket which is return 0;
int main() { // Write your main code here return 0; }
Main Code:
Now after basic code, we will write Main Code. In our case we want to print our text "Hello World". For it we will write built-in command cout which is present in our built-in standard library iostream. This command require some extra things for its syntax. So for it, first enter two less than marks << and after it write two quotation marks " " and then between these two marks we write our text which we want to display on console and then enter semicolon ; after second quotation mark.
cout<<"Hello World !";
Source Code & Output:
The source code and output of our topic is given below so that you can easily practice and understand it by running code on your computer and verify its output.
Source Code:
The source code of "First Program Hello World in C++" is below.
// First Program Hello World by Muhammad Usman on Search 2 Programming 247 (Twenty-Four Seven)
#include <iostream> // built-in library to use cin and cout commands
using namespace std; // part of above library to use standard library to avoid same name commands, functions
int main()
{
// cout built-in command to displays the string/text
cout<<"Hello World !";
return 0;
}
Output:
The output of "First Program Hello World in C++" is below.
Hello World !
Dry Run With Example:
We can print any our desired text on console in same way as in above source code we printed Hello World. You can also print anything by entering in printf() function for example:
cout<<"Today I took my 1st lesson on C++ Programming.";
Then the built-in function printf() will display the text "Today I took my 1st lesson on C++ Programming." your on console screen.
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello World ! This is the C++ first program.";
}
