Programs

C++ “Hello, World!” Program

A basic “Hello, World!” program in C++ is shown below:

#include <iostream>

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

Here’s a brief explanation of the code:

  1. #include <iostream>: This directive includes the standard C++ header file that defines the input-output stream objects. The most common object you’ll use from this header is std::cout for console output.
  2. int main(): Every C++ program starts execution from the main function.
  3. std::cout << "Hello, World!" << std::endl;: This line prints the “Hello, World!” string to the console. The std::endl is an end-of-line character, ensuring that the output is followed by a newline.
  4. return 0;: This statement exits the main function and returns a status code of 0 to the operating system, indicating successful execution.

To run the program:

  1. Save the code in a file named hello.cpp.
  2. Compile the program using a C++ compiler, for instance, g++:
g++ hello.cpp -o hello
  1. Execute the compiled program:
./hello

You should see “Hello, World!” printed to the console.