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:
#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 isstd::coutfor console output.int main(): Every C++ program starts execution from themainfunction.std::cout << "Hello, World!" << std::endl;: This line prints the “Hello, World!” string to the console. Thestd::endlis an end-of-line character, ensuring that the output is followed by a newline.return 0;: This statement exits themainfunction and returns a status code of 0 to the operating system, indicating successful execution.
To run the program:
- Save the code in a file named
hello.cpp. - Compile the program using a C++ compiler, for instance,
g++:
g++ hello.cpp -o hello
- Execute the compiled program:
./hello
You should see “Hello, World!” printed to the console.
