How to Print “Hello, World!” in C++ Programming Language




“Hello, World!” in C++ programming language:


#include <iostream>


int main() {

    std::cout << "Hello, World!";

    return 0;

}


Breakdown of the Program

Let’s break down the code to understand how it works:

  • #include <iostream>: This is a preprocessor command that includes the input-output stream iostream.
  • int main(): This is the main function where the program execution begins.
  • {}: Curly braces define the body of the function.
  • std::cout << "Hello, World!";: std::cout is used to print the “Hello, World!” string on the screen.
  • return 0;: This is the “Exit status” of the program. In simple terms, the program ends with this statement.

How to Run the Program

  1. Open a text editor and add the above-mentioned code.
  2. Save the file as HelloWorld.cpp.
  3. Open a terminal and go to the directory where you saved the file.
  4. Type g++ HelloWorld.cpp and press enter to compile your code.
  5. If there are no errors in your code, the command prompt will take you to the next line and would generate a.out executable file.
  6. Now, type ./a.out to run your program.
  7. You will see “Hello, World!” printed on the screen.

Congratulations! You’ve just created and executed your first C++ program.

Happy coding! 🎉

Post a Comment

0 Comments