Hello World Program in C++

 


The "Hello, World!" program is the simplest way to get started with C++. It helps you understand basic syntax, compilation, and execution.


Code:

#include <iostream>  // Include input-output stream library
using namespace std; // Use standard namespace

int main() {
    cout << "Hello, World!" << endl; // Print Hello, World!
    return 0;  // Return 0 to indicate successful execution
}

Explanation:

  1. #include <iostream>

    • This is a preprocessor directive that includes the iostream library, which allows input and output operations.
  2. using namespace std;

    • This tells the compiler to use the standard namespace, so we can use cout and cin without prefixing them with std::.
  3. int main() { ... }

    • This is the main function, the entry point of any C++ program.
  4. cout << "Hello, World!" << endl;

    • cout is used to display output on the screen.
    • << is the insertion operator.
    • "Hello, World!" is a string that gets printed.
    • endl moves the cursor to the next line.
  5. return 0;

    • It indicates that the program ran successfully.

How to Compile & Run:

Using a Compiler (G++)

  1. Write the program in a file, e.g., hello.cpp
  2. Compile the program using:
    g++ hello.cpp -o hello
    
  3. Run the program using:
    ./hello
    

Output:

Hello, World!

Would you like to add step-by-step execution details for beginners? 🚀

Post a Comment

0 Comments