The "Hello, World!" program is a classic and simple program that is often used to introduce a new programming language. In the C++ programming language, the "Hello, World!" program is typically written as follows
Create a file
- Open a text editor or IDE. You will be writing your C++ code in this text editor or IDE.
- Start a new file and save it with a
.cpp
extension. For example,hello.cpp
. - Write the below code in your text editor or IDE:
- Save the file
Code
C++
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
#include <iostream>
: This line includes theiostream
library, which provides input/output functions such ascout
.int main()
:main
is the entry point of a C++ program. It returns an int value and takes no arguments.std::cout << "Hello, World!" << std::endl;
: This line uses thecout
function from theiostream
library to print the string"Hello, World!"
to the console. Thestd::endl
at the end adds a new line character.return 0;
: The return 0 statement signifies that the program executed successfully and returns the value0
to the operating system.
Run in GCC Compiler
- Open a terminal or command prompt window and navigate to the directory where you saved your
.cpp
file. - Compile your code by typing
g++ hello.cpp -o hello
and pressing Enter. This command compiles the code using the g++ compiler and outputs an executable file named hello. - Run your program by typing
./hello
and pressing Enter.
Run in Turbo C++ Compiler
To run the "Hello, World!" program in Turbo C++, you can follow these steps:
- Open the Turbo C++ IDE (Integrated Development Environment) by double-clicking on the Turbo C++ icon or running the TC.exe file in the Turbo C++ installation folder.
- Go to the "File" menu and select "New" to create a new file. This will open an editor window where you can type in the C code for the "Hello, World!" program.
- Copy and paste or Type the code from the previous example into the editor window:
- Go to the "File" menu and select "Save As". Select "All files" and give your file a name with the
".cpp"
extension (e.g. "hello.cpp") - Go to the "Compile" menu and select "Compile" (or press
F9
on the keyboard) to compile the program. This will check the code for any syntax errors. If there are any errors, they will be displayed in a separate window, and you will need to fix them before proceeding. - If the program compiles successfully, go to the "Run" menu and select "Run" (or press
Ctrl + F9
) to execute the program.
And that's it! You have written your first "Hello, World!" program in C++.