Your First C++ Program
Welcome to the world of C++ programming! If you're just starting out with C++, the best way to begin is by writing a simple program that displays a message on the screen. This "Hello, World!" program is a traditional first program for beginners learning any language, and it will introduce you to the basic syntax and structure of C++.
C++ is a powerful, high-performance programming language that is widely used in fields such as software development, game programming, embedded systems, and systems programming. It builds on the foundation of the C language but adds object-oriented features that allow developers to write modular and reusable code.
The first program we’ll write in C++ simply prints the text "Hello, World!" to the console. Here’s how you can do it.
You need a C++ compiler to turn your code into a runnable program. Popular C++ compilers include GCC, Clang, and Microsoft Visual C++. Most C++ programmers use an Integrated Development Environment (IDE) to make coding easier, such as:
Alternatively, you can write C++ code in a simple text editor (like Sublime Text or VS Code) and compile it using the terminal or command prompt.
Open your IDE or text editor, and create a new file with the .cpp
extension. For example, name it hello_world.cpp
.
Now, let's write the C++ code. Here’s the simplest C++ program that prints "Hello, World!" to the console:
#include <iostream> // Include the input-output stream library
using namespace std; // Use the standard namespace
int main() { // Main function where the execution starts
cout << "Hello, World!" << endl; // Output the text to the console
return 0; // Exit the program with a successful status
}
#include <iostream>
:
cout
function.using namespace std;
:
std
namespace is the standard C++ library. By including this line, we can use standard library features (like cout
) without having to type std::
before them.int main()
:
main()
function, where execution begins. The int
before main()
means that this function will return an integer value to the operating system when it finishes. A return value of 0
typically indicates successful execution.cout << "Hello, World!" << endl;
:
cout
is the standard output stream, which displays text on the console.<<
operator is used to send data to the output stream (in this case, the string "Hello, World!"
).endl
is a special manipulator that adds a newline to the output, so the cursor moves to the next line.return 0;
:
main()
function and returns a status code of 0, indicating that the program executed successfully.Save the file as hello_world.cpp
in a directory on your computer.
Open the terminal or command prompt:
Navigate to the directory where you saved the file using the cd
command. For example:
cd path/to/your/file
Compile the program:
g++ -o hello_world hello_world.cpp
This tells the GCC compiler to compile hello_world.cpp
and create an executable file named hello_world
.
Run the program:
hello_world.exe
./hello_world
Expected Output:
Hello, World!
cout << "Hello, World!
(missing closing quote).