Writing and Running Your First C++ Program

Introduction

In the previous chapter, we have covered the necessary steps to set up your development environment. Now, it’s time to write and run your first C++ program. This chapter will guide you through the process of writing, compiling and executing your first C++ program using both a text editor and Visual Studio Code (VS Code).

Writing and Running Your First C++ Program

Step-by-Step Guide Using a Text Editor

1. Writing the Program

  1. Open a text editor: Use any text editor you are comfortable with, such as Notepad, Sublime Text, or Atom.
  2. Create a new file: Create a new file and name it hello.cpp.
  3. Write the code: Copy and paste the following code into hello.cpp:
    #include <iostream> // Preprocessor directive to include the input-output stream library
    
    int main() {
        std::cout << "Hello, World!" << std::endl; // Print "Hello, World!" to the console
        return 0; // Return 0 to indicate successful execution
    }
    
  4. Save the file: Save the file to your working directory.

2. Compiling and Running the Program

On Linux or macOS
  1. Open a terminal: Open the terminal application on your computer.
  2. Navigate to the directory: Use the cd command to navigate to the directory where hello.cpp is saved.
    cd path/to/your/directory
    
  3. Compile the program: Use the following command to compile the program:
    g++ -o hello hello.cpp
    

    This command tells the compiler to create an executable file named hello from the source code file hello.cpp.

  4. Run the executable: Use the following command to run the executable:
    ./hello
    

    You should see the following output:

    Hello, World!
    
On Windows
  1. Open a command prompt: Open the command prompt application on your computer.
  2. Navigate to the directory: Use the cd command to navigate to the directory where hello.cpp is saved.
    cd path\to\your\directory
    
  3. Compile the program: Use the following command to compile the program:
    g++ -o hello hello.cpp
    

    This command tells the compiler to create an executable file named hello.exe from the source code file hello.cpp.

  4. Run the executable: Use the following command to run the executable:
    hello
    

    You should see the following output:

    Hello, World!
    

Step-by-Step Guide Using Visual Studio Code

1. Setting Up Visual Studio Code

Refer to the previous chapter for setting up Visual Studio Code IDE.

2. Writing the Program

  1. Open Visual Studio Code: Launch Visual Studio Code.
  2. Create a new file: Click on File > New File and name it hello.cpp.
  3. Write the code: Copy and paste the following code into hello.cpp:
    #include <iostream> // Preprocessor directive to include the input-output stream library
    
    int main() {
        std::cout << "Hello, World!" << std::endl; // Print "Hello, World!" to the console
        return 0; // Return 0 to indicate successful execution
    }
    
  4. Save the file: Save the file to your working directory.

3. Compiling and Running the Program

  1. Open the terminal in VS Code: Open the integrated terminal in VS Code by going to View > Terminal or by pressing Ctrl+ (backtick).
  2. Navigate to the directory: Use the cd command to navigate to the directory where hello.cpp is saved.
    cd path/to/your/directory
    
  3. Compile the program: Use the following command to compile the program:
    g++ -o hello hello.cpp
    

    This command tells the compiler to create an executable file named hello from the source code file hello.cpp.

  4. Run the executable: Use the following command to run the executable:
    ./hello
    

    (On Windows, use hello instead of ./hello.)

    You should see the following output:

    Hello, World!
    

Conclusion

In this chapter, we covered the process of writing and running your first C++ program using both a text editor and Visual Studio Code. You learned how to create a simple “Hello, World!” program, compile it, and execute it. This foundational knowledge will enable you to start developing more complex C++ programs. In the next chapter, we will delve into the structure of a C++ program, exploring its various components in detail.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top