Read Time:1 Minute, 18 Second
C++ Hello World Program
Here’s a simple “Hello, World!” program in C++:
#include <iostream> using namespace std; int main() { cout<<"Hello, World!"<<endl; return 0; }
Let’s break down what’s happening here:
- The first line
#include <iostream>
is a preprocessor directive that tells the compiler to include the iostream library. This library provides input/output functionality, such as printing output to the console. - The
int main()
function is the entry point of the program. This is where the program starts executing. - The line
std::cout << "Hello, World!" << std::endl;
prints the text “Hello, World!” to the console. The<<
operator is used to write data to the console, andstd::endl
is used to insert a newline character after the text. - The
return 0;
statement ends the main function and returns the value 0 to the operating system. A return value of 0 indicates that the program executed successfully.
To run this program, you will need a C++ compiler installed on your system. Once you have a compiler installed, save the code above in a file with a .cpp extension (for example, helloworld.cpp). Then, open a terminal or command prompt, navigate to the directory where the file is saved, and enter the following command to compile the program:
g++ helloworld.cpp -o helloworld
This will compile the program and generate an executable file called “helloworld”. To run the program, enter the following command:
./helloworld
This will execute the program and print the message “Hello, World!” to the console.