cplusplus
cplusplus

 

In this tutorial, we will explore one of the simplest programs you can write in C++: printing “Hello, World!” to the console. This is often the first program a beginner writes when learning a new programming language.

The primary objective of this topic is to introduce the basic structure of a C++ program, understand how to use the `cout` object, and gain familiarity with the syntax required to produce output in the console.

Objective

  • Understand the structure of a basic C++ program.
  • Learn how to use the `#include` directive to include libraries.
  • Use the cout object to display text in the console.

Code Example


#include <iostream>

int main() {
    // Print "Hello, World!" to the console
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

Explanation of the Program

Let’s break down the components of this program:

  • #include <iostream>: This line tells the compiler to include the iostream library, which provides functionality for input and output operations. Specifically, we need it for std::cout, which will be used to print output to the console.
  • int main(): This is the starting point of any C++ program. The main() function is where the program execution begins. It is a required function, and its return type is usually int to indicate the success or failure of the program.
  • std::cout << “Hello, World!” << std::endl;: This line is the key to printing output. std::cout is an object used to output data to the console. The << operator is used to send the string “Hello, World!” to std::cout, and std::endl inserts a new line after the output.
  • return 0;: This statement marks the end of the main function. The value 0 is returned to the operating system to indicate that the program has executed successfully.

How to Run the Program

  1. Open a text editor (such as VSCode, Sublime Text, or even Notepad).
  2. Create a new file and save it with a .cpp extension, for example, hello_world.cpp.
  3. Copy and paste the C++ code into your file.
  4. To compile and run the program, you need a C++ compiler such as g++. Open a terminal and navigate to the folder where you saved your file.
  5. Run the following command to compile the code:
    g++ hello_world.cpp -o hello_world
  6. After the program is compiled successfully, run the program using the following command:
    ./hello_world
  7. In the terminal, you should now see the output:
    Hello, World!

Conclusion

You’ve now written and run a basic “Hello, World!” program in C++. This is an essential first step in learning the language, and it introduces you to fundamental programming concepts like input/output operations, functions, and program structure.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)