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
- Open a text editor: Use any text editor you are comfortable with, such as Notepad, Sublime Text, or Atom.
- Create a new file: Create a new file and name it
hello.cpp. - 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 } - Save the file: Save the file to your working directory.
2. Compiling and Running the Program
On Linux or macOS
- Open a terminal: Open the terminal application on your computer.
- Navigate to the directory: Use the
cdcommand to navigate to the directory wherehello.cppis saved.cd path/to/your/directory - Compile the program: Use the following command to compile the program:
g++ -o hello hello.cppThis command tells the compiler to create an executable file named
hellofrom the source code filehello.cpp. - Run the executable: Use the following command to run the executable:
./helloYou should see the following output:
Hello, World!
On Windows
- Open a command prompt: Open the command prompt application on your computer.
- Navigate to the directory: Use the
cdcommand to navigate to the directory wherehello.cppis saved.cd path\to\your\directory - Compile the program: Use the following command to compile the program:
g++ -o hello hello.cppThis command tells the compiler to create an executable file named
hello.exefrom the source code filehello.cpp. - Run the executable: Use the following command to run the executable:
helloYou 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
- Open Visual Studio Code: Launch Visual Studio Code.
- Create a new file: Click on
File>New Fileand name ithello.cpp. - 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 } - Save the file: Save the file to your working directory.
3. Compiling and Running the Program
- Open the terminal in VS Code: Open the integrated terminal in VS Code by going to
View>Terminalor by pressingCtrl+(backtick). - Navigate to the directory: Use the
cdcommand to navigate to the directory wherehello.cppis saved.cd path/to/your/directory - Compile the program: Use the following command to compile the program:
g++ -o hello hello.cppThis command tells the compiler to create an executable file named
hellofrom the source code filehello.cpp. - Run the executable: Use the following command to run the executable:
./hello(On Windows, use
helloinstead 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.