Hello World Program in C++ with Code Explanation (2024)

By : Barbara Thompson

Updated

Hello World Program in C++

The “Hello World” program is the first but most vital step towards learning any programming language and it is certainly the simplest program you will learn with each programming language. All you need to do is display the message “Hello World” on the output screen.

Let us now look at C++ Hello World Code:

Step 1) On configuration page. Select create the cache now option.

You should see a screen something like this

In some computers and operating systems, it asks whether to include all the libraries. If the option is selected, it will install all the libraries.

Step 2) Create a new source file.
Once the program opens, you need to create a new source file, so you can start writing your first C++ program. To do this select File > New > Source File. The path is shown in the figure below.

This will open an area where you be able to type out your code.

Step 3) Now you can write the C++ code. After that you can write the C++ code as shown in the image below:

C++ Code Example:

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

Step 4) Compile your code. In this step, Click on Execute->compile & run

Step 5) Save the file. After saving you should see a black screen outputting “Hello World.”

Output

Your First Program: C++ “Hello World!” Explanation

C++ is a compiled language. The source code is compiled into object files. Object files are then combined by a linker creating an executable program.

A production C++ consists of many source code files (usually called source files).

  • Curly braces, {}, express grouping in C++. Here, they indicate the start and end of the function body.
  • Every C++ program has exactly one global function named main (). The program starts by executing that function. An int value is returned by main (), which it passes to the system.’ If no value is returned, then the system will receive a value 0 thus indicating successful completion. A nonzero value from the main () function indicates failure.

Explanation of C++ Hello World Program Code

Code line 1: The first line is #include <iostream>. It instructs the compiler to include the standard stream I/O library. Without this header inclusion the expression would not compile.

std::cout << "Hello, World"<<endl

Code line 4: int main(). This is the main function of the program. Functions are denoted by the parentheses(). Before the main function is “int”. This means that the main function will return an integer to the function or process that called it.

Don’t worry about this, for the time being, simply note that the program must return an integer before the end. The curly braces, { and }, contain the code within a function. The program terminates, at the end of the main function denoted by }

Code line 6: The operator << writes its second argument onto its first. In this case, the string literal “Hello, World!” is written onto the standard output stream std:: cout.

(Note: A string literal is a sequence of characters surrounded by double quotes. endl inserts a newline character on the same line)

Code line 7: return 0; This is the last command in the main function, the return statement. Its purpose is only to return a value to the function or process that is called the main function. Don’t worry about this other than the fact that it is required by the “int” in front of the main function definition. It should return a zero from the main function meaning the program ran successfully and exited.

cout<<"Hello World"<<endl;

Note: Cout is a stream which outputs to the stream specified. It is by default the standard output stream. Cout is very common in programs as the ultimate motive in every program is to give some output. endl; represents the end of statements in C++. The semicolon in C++ separates different statements and must be put at the end of statements in C++.

Summary

  • The “Hello World” program is the first step towards learning any programming language.
  • After installing a C++ compiler and a Text Editor of your choice, you can go ahead and execute your first basic C++ program.
  • The first line is #include . It instructs the compiler to include the standard stream I/O library.
  • : int main(). This is the main function of the program.
  • The operator << writes its second argument onto its first.
  • Return 0; is the last command in the main function which is the return statement.
  • : Cout is a stream that outputs the stream specified.

You Might Like:

  • C++ Polymorphism with Example
  • C++ Operator Overloading with Examples
  • How to Download and Install C++ IDE on Windows
  • C++ Tutorial for Beginners: Learn Programming Basics in 7 Days
  • Difference between Structure and Class in C++
  • C++ Tutorial PDF for Beginners (Download Now)
  • Static Member Function in C++ (Examples)
Hello World Program in C++ with Code Explanation (2024)

FAQs

How to write Hello World using C++? ›

How To Make a Computer Say Hello
  1. #include <iostream>
  2. int main() {
  3. std::cout << "Hello World! Welcome to your first C++ program!" << std::endl; }
Sep 20, 2021

How to run Hello World in C++ in VS Code? ›

Run helloworld.cpp
  1. Make sure you have helloworld.cpp open so it is the active file in your editor.
  2. Press the play button in the top right corner of the editor.
  3. Choose C/C++: g++.exe build and debug active file from the list of detected compilers on your system.
Jul 24, 2023

How do you write Hello World in code? ›

Basic example: Creating and running “Hello World”
  1. Create the following C program and name the source file hello.c : #include <stdio.h> int main(void) { printf("Hello World!\n"); return 0; }
  2. Compile the program: ...
  3. Run the program by entering the following command: ./hello.

What is C++ programming with an example? ›

C++ (or “C-plus-plus”) is a generic programming language for building software. It's an object-oriented language. In other words, it emphasizes using data fields with unique attributes (a.k.a. objects) rather than logic or functions. A common example of an object is a user account on a website.

Where to write C++ code? ›

An IDE (Integrated Development Environment) is used to edit AND compile the code. Popular IDE's include Code::Blocks, Eclipse, and Visual Studio. These are all free, and they can be used to both edit and debug C++ code.

How to run C++ code? ›

Run your code in a command window
  1. To open a command prompt window, press Windows+R to open the Run dialog. Enter cmd.exe in the Open textbox, then choose OK to run a command prompt window.
  2. In the command prompt window, right-click to paste the path to your app into the command prompt. Press Enter to run your app.
Mar 20, 2024

How to compile C++ code? ›

Compile and Execute C++ Program
  1. Open a text editor and add the code as above.
  2. Save the file as: hello.cpp.
  3. Open a command prompt and go to the directory where you saved the file.
  4. Type 'g++ hello. cpp' and press enter to compile your code. ...
  5. Now, type 'a. ...
  6. You will be able to see ' Hello World ' printed on the window.

How to setup C++ in VS Code? ›

Prerequisites
  1. Install Visual Studio Code.
  2. Install the C/C++ extension for VS Code. You can install the C/C++ extension by searching for 'C++' in the Extensions view (Ctrl+Shift+X).

How to run code in VS Code? ›

To run or debug a simple app in VS Code, select Run and Debug on the Debug start view or press F5 and VS Code will try to run your currently active file. However, for most debugging scenarios, creating a launch configuration file is beneficial because it allows you to configure and save debugging setup details.

Why is Hello World used in coding? ›

A "Hello, World!" program is often the first written by a student of a new programming language, but such a program can also be used as a sanity check to ensure that the computer software intended to compile or run source code is correctly installed, and that its operator understands how to use it.

How to say Hello World in C code? ›

printf("Hello World \n"); printf("hey! welcome you all"); In the above code, we have mentioned \n in the printf() to display the output in the new line.

How to write C++ code step by step? ›

Write a C++ program

Lines beginning with a hash sign (#) are directives read and interpreted by what is known as the preprocessor. Line 2− A blank line: Blank lines have no effect on a program. Line 3− We then declare a function called main with the return type of int. main() is the entry point of our program.

Can I start C++ as a beginner? ›

Yes, you can learn C++ as a beginner, but it will take longer than if you already have a firm grasp of programming. If you have never programmed before, look for a C++ course that is designed with beginners in mind.

Which C++ is best for beginners? ›

If so, Codio's edX course, C++ Programming: Basic Skills, is the perfect fit for you. It's designed for complete beginners to coding, with no prior programming experience required. By the end of the course, you'll have a solid foundation in computer science and programming.

How to print Hello World in C++ using class? ›

class HelloWorld { public: void PrintHelloWorld() { std::cout << "Hello World! \n"; } }; In this Hello World C++ code example, we have created a class “HelloWorld” with one public function PrintHelloWorld(). Public function means that other functions outside this class may call it directly.

How to print Hello World in double quotes in C++? ›

How to print "hello world"(including double quote) in c++?
  1. + 7. By using the \ escape character. std::cout << "\"Hello World\""; ...
  2. + 2. https://code.sololearn.com/ctj1Bh48ZIT4/?ref=app. 12th Aug 2017, 6:03 AM. ...
  3. + 2. printf("\"Hello World ! \""); 12th Aug 2017, 6:03 AM. ...
  4. cout<<"\"Hello World\""; 12th Aug 2017, 6:09 AM.
Aug 12, 2017

References

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Greg O'Connell

Last Updated:

Views: 6037

Rating: 4.1 / 5 (62 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Greg O'Connell

Birthday: 1992-01-10

Address: Suite 517 2436 Jefferey Pass, Shanitaside, UT 27519

Phone: +2614651609714

Job: Education Developer

Hobby: Cooking, Gambling, Pottery, Shooting, Baseball, Singing, Snowboarding

Introduction: My name is Greg O'Connell, I am a delightful, colorful, talented, kind, lively, modern, tender person who loves writing and wants to share my knowledge and understanding with you.