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:
-
#include <iostream>
- This is a preprocessor directive that includes the iostream library, which allows input and output operations.
-
using namespace std;
- This tells the compiler to use the standard namespace, so we can use
cout
andcin
without prefixing them withstd::
.
- This tells the compiler to use the standard namespace, so we can use
-
int main() { ... }
- This is the main function, the entry point of any C++ program.
-
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.
-
return 0;
- It indicates that the program ran successfully.
How to Compile & Run:
Using a Compiler (G++)
- Write the program in a file, e.g.,
hello.cpp
- Compile the program using:
g++ hello.cpp -o hello
- Run the program using:
./hello
Output:
Hello, World!
Would you like to add step-by-step execution details for beginners? 🚀
0 Comments