“Hello, World!” in C++ programming language:
#include <iostream>
int main() {
std::cout << "Hello, World!";
return 0;
}
Breakdown of the Program
Let’s break down the code to understand how it works:
- #include <iostream>: This is a preprocessor command that includes the input-output stream iostream.
- int main(): This is the main function where the program execution begins.
- {}: Curly braces define the body of the function.
- std::cout << "Hello, World!";: std::cout is used to print the “Hello, World!” string on the screen.
- return 0;: This is the “Exit status” of the program. In simple terms, the program ends with this statement.
How to Run the Program
- Open a text editor and add the above-mentioned code.
- Save the file as HelloWorld.cpp.
- Open a terminal and go to the directory where you saved the file.
- Type g++ HelloWorld.cpp and press enter to compile your code.
- If there are no errors in your code, the command prompt will take you to the next line and would generate a.out executable file.
- Now, type ./a.out to run your program.
- You will see “Hello, World!” printed on the screen.
Congratulations! You’ve just created and executed your first C++ program.
Happy coding! 🎉
0 Comments