CodingHowTo

Writing a Simple Hello World Program in C++

In this guide, we'll walk you through the process of creating and compiling a simple "Hello World" program in C++ using the command line on Linux. Whether you're new to programming or learning C++, this step-by-step tutorial will help you get started.

Step 1: Write the Code

Open your terminal and use a text editor like nano to create a new C++ file. For example:

nano hello.cpp

Example Code: "Hello World" in C++

#include <iostream>

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

Save the file by pressing `CTRL + X`, then `Y` to confirm changes, and `Enter` to exit.

Step 2: Compile the Code

Use the GNU C++ compiler (g++) to compile your program. In your terminal, navigate to the directory where you saved `hello.cpp` and run:

g++ hello.cpp -o hello

Step 3: Run the Program

After successful compilation, you can run your program by typing:

./hello

You should see the output:

Hello, World!

Conclusion

Congratulations! You've just written and executed your first C++ program on Linux using the command line. This simple "Hello World" example demonstrates some fundamental aspects of C++ programming, such as including libraries, using input/output streams, and structuring a basic program with a `main` function.

As you continue to learn C++, you'll explore more complex concepts like variables, data types, control structures, functions, classes, and object-oriented programming. Keep practicing, and soon you'll be writing sophisticated applications!

ß