CPP

Your First C++ Program - Hello, World!


Congratulations on taking your first step into C++ programming! In this guide, we will walk you through creating and running your first C++ program, the classic `Hello, World!` program.

What is `Hello, World!`?

The `Hello, World!` program is a simple tradition in the programming world. It's often the first program beginners write in a new programming language. The goal is to display the text `Hello, World!` on the screen. It's a basic exercise to familiarize yourself with the syntax and structure of the language.

Creating the Program

Let's write the C++ code for the `Hello, World!` program:

#include <iostream>
int main() {
    std::cout << `Hello, World!` << std::endl;
    return 0;
}
    

This code does the following:

  • Includes the <iostream> library, which provides input and output functionality.
  • Defines the main() function, which is the entry point of your C++ program.
  • Uses std::cout to display `Hello, World!` on the screen.
  • Returns 0 to indicate a successful program execution.

Compiling and Running

Now that you've written your code, it's time to compile and run it. Follow these steps:

  1. Save the code in a file with a .cpp extension (e.g., hello.cpp).
  2. Open your command prompt or terminal.
  3. Navigate to the directory where the file is saved.
  4. Compile the program using the following command:
g++ hello.cpp -o hello
    

This command uses the GNU C++ compiler (g++) to compile your code and generates an executable file called hello.

  1. Run the program by executing:
./hello
    

You should see `Hello, World!` displayed on your screen. Congratulations, you've successfully written and run your first C++ program!

What's Next?

Now that you've completed your first C++ program, you can explore more advanced C++ topics, such as variables, functions, and object-oriented programming. Continue learning, practicing, and building exciting applications using C++.

Written by Surfside Media

Senior Full Stack Developer specializing in Web Technologies.