First C++ Program

One of the most popular programming languages for creating competitive programming, games, and system applications is C++. It has strong features like memory management, low-level manipulation, and object-oriented programming. This blog will guide you through creating a basic “Hello, World!” C++ application and explain its fundamental architecture.

Getting Started: Setting Up the Environment

You’ll need a compiler before we write our first application. Your code becomes executable when a compiler converts it into a format that is machine-readable. An array of IDEs (Integrated Development Environments) are available for use, such as Code::Blocks, Visual Studio, and Dev-C++. Alternatively, you can use Sublime Text or VS Code, which are lightweight text editors, and utilise command-line tools to compile your code.

For this tutorial, we’ll use a simple text editor and the g++ compiler, which is commonly available on all platforms.

Writing Your First C++ Program

Immediately, let’s write a basic program that outputs “Hello, World!” to the console. The most conventional method for beginning to learn any new programming language is as follows.

Here’s the code for the first program:

#include <iostream>

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

Code Breakdown

Now, let’s break down what’s happening in the code line by line.

  1. #include <iostream>: This line includes a standard library called iostream. The iostream library provides functionalities for input (cin) and output (cout) in C++. This is necessary to use std::cout for displaying output to the console.
  2. int main(): Every C++ program starts from the main function. It is the entry point of the program, meaning that execution starts from here. The keyword int indicates that this function will return an integer value.
  3. std::cout << "Hello, World!" << std::endl;:
    • std::cout is the output stream used to print text to the console. It’s part of the iostream library.
    • << is the stream insertion operator that pushes the text “Hello, World!” into the output stream.
    • std::endl is used to insert a newline character, ensuring that the next output appears on a new line.
  4. return 0;: The main function returns an integer value, usually 0, to indicate that the program has run successfully.

How to Compile and Run the Program

Compile: After writing the code, you need to compile it. Open your terminal or command prompt, navigate to the folder where your code file is saved (let’s assume you named it first_program.cpp), and use the following command:

g++ first_program.cpp -o first_program

By doing this, the program will be compiled and the executable file first_program will be created.

Video Tutorial:
C++ First Program

Run: After the compilation, you can run the program using:

  • On Linux/macOS:
./first_program
  • On Windows:
first_program.exe

If everything works correctly, the output will be:

Hello, World!

Understanding Important Concepts

Comments: Comments in C++ are used to explain the code. They are ignored by the compiler. You can write single-line comments using // or multi-line comments using /* */.

Example:

// This is a single-line comment
/* This is a 
   multi-line comment */

Standard Namespace: In the program, you noticed std::cout. The std is the standard namespace in C++. You can avoid writing std:: every time by using:

using namespace std;

However, it’s generally a good practice to explicitly use std:: to avoid name conflicts in larger projects.

Return Values in main(): The main function must return an integer value, where 0 typically means that the program executed successfully. If there’s an error, you can return other values to indicate different issues.

Conclusion

Best wishes! Your first C++ program has been developed and executed successfully. Despite being a straightforward “Hello, World!” example, it teaches you to the main function, libraries, output streams, and compilation that make up a C++ program.

Though C++ is a large language, you can write simple programs and eventually create sophisticated applications with constant practice. We’ll go over more fundamental subjects like variables, data types, loops, functions, and object-oriented programming in later tutorials.

Learn More:
Features of C++

Leave a Comment