How to Write First C++ Program, Example Hello World

By | October 16, 2023

Varun Saharawat is a seasoned professional in the fields of SEO and content writing. With a profound knowledge of the intricate aspects of these disciplines, Varun has established himself as a valuable asset in the world of digital marketing and online content creation.


hello world c++

First C++ program: C++ is one of the famous programming languages widely used in the tech world. It combines features of both high-level languages, which makes it suitable for a wide range of applications. Although writing your first program is a fascinating and motivating experience, it can also be hard. However, we will start our coding journey by writing the Hello World program. Learning a language like C++ can be an excellent start if you are new to coding.

However, you must be prepared for the local environment to execute your first C++ program. Our first program will display “Hello World” in C++. Today, we will help you write your first C++ program in this blog. So buckle up and get ready to take on your coding journey.

Why Choose C++?

C++ is a widely popular programming language used by various programmers, whether they are beginners or experts. The syntax used in C++ is the same as that of the C program. C++ is an extension of the C language. Its user-friendly and robust syntax make it an excellent choice for programmers. It has huge community support and STL modules to make coding easier for you. 

Learning C++ helps you develop essential skills and a solid foundation to dive into large projects quickly. Although it is a stepwise process, move ahead one step at a time. We need to go individually and follow the process with a lot of practice. We will start with our first C++ program, “Hello World”. 

Writing Your First C++ Program

Our first requirement while writing our first C++ program is a development environment. We need a system and a compiler set up in the environment, which will help to execute our code. There are various C++ compilers available that we can use. These IDEs come loaded with various features, like syntax highlighting, autocode completion, compiling, and many more. Nowadays, you can also use various online compilers to compile your code without worrying about any other requirements.

1. Setting Up Your Development Environment

Many users work in different environments that require proper IDEs for the code to function. There are three operating systems, namely Windows, Mac, and Linux. Users need to set up their environment before starting to code on their computer system.

  • Windows users can download various IDEs available for free on the internet, such as Code::Blocks or Visual Studio Community Edition.
  • MacOS users need to Install Xcode Command Line Tools by running 

                    xcode-select –install in the Terminal.

  • Linux users need to open their Terminal and type 

                      sudo apt-get update

                      sudo apt-get install g++ 

This will install the GNU Compiler Collection on their local system.

2. Working of First C++ Program Hello World

Let us break down our C++ program and understand the needs of all the terminologies used in our program.

1. Use of // (Comments) in the program

We can use comments in our C++ program to display any important information related to our program. Comments used inside the compiler do not get compiled. 

Our compiler will skip comments while compiling. To use comments, we start writing before appending the “//” syntax. However, we also use /*—-*/ for using comments in C++. Any sentence or code enclosed within these quotes is skipped and is not compiled.

Use of comments in Program
#include <iostream>

int main( ) {

    // This is a single-line comment

    cout << “Hello, World!” << endl; 

    return 0; // This comment explains the return statement

}

Also, read How to Add Comments in CSS

2. #Include <iostream>

With the help of #include<iostream>, you can access objects like ‘cout,’ which is used to display the program output by sending it to the standard output device. It instructs the compiler to add the package file in our source code program. It is used to provide input and output operations in C++ programs.

Writing First C++ Program
    cout << “Hello, World!” << endl;

3. Using namespace std

This line is used in our C++ programs to simplify our code by using standard C++ identifiers without using std namespace each time. The std namespace is very large, which makes the code complex. Importing the namespace std eliminates the repetitive use of std:: in our code. 

                      std::cout << “Hello, World!” << std::endl; 

It helps us to write input and output commands such as ‘cin’ and ‘cout’ directly instead of ‘std::cin’ and ‘std::cout’. 

4. Int main( ) 

It is the starting point of every C++ program. It is a particular function where the program execution begins. When you run your first C++ program, the operating system will first call the main ( ) function and then execute the statement sequentially. 

                                        main( )

Also, the int used before the main() function indicates that our main function returns an integer value. No function or method outside the main ( ) function is executed until the main () function is not executed. 

5. Cout<< “Hello World”<<endl;

In C++, cout is an output stream that displays the program’s output on the terminal window.               

                        Cout<< “Hello World”<<endl;

In our program, the use of the cout method is to display the text enclosed inside the double quotes, “Hello World.” 

                                    <<endl

Also, the << operator inserts the string into the output stream. The endl used at the end represents the end-of-line, which takes the cursor to the following line. With the help of cout, you can display strings and variables. Expressions, and other data types.

6. return 0

In C++ programming, return 0 is used at the end of the main() function to indicate that the program completed its execution successfully. When a C++ program finishes its execution, it sends the status back to the system with the help of return 0. It indicates that the program executed without any errors.

3. Writing our First C++ Program

Let us now finally write our first C++ program that will display “Hello World” on the console screen. 

Writing First C++ Program
//Writing C++ program that displays Hello World

#include <iostream>

using namespace std;

// Main( ): Execution of our program start from here in C++

int main() {

    // Print Hello World 

    cout << “Hello, World!” << endl;

    return 0;

}

Run the program by clicking on the run button of the compiler, which converts the instruction into binary machine code and executes the code. If you need to execute the program on a compiler, then write the following command.

The output of the above program will be “Hello World” on our compiler screen.

Output
Hello World

Compiling Your First C++ Program

After completing your code, you must compile the program to produce the desired output. 

If you are working in Visual Studio, you need to save your code first and press ‘Ctrl +F5’ to compile and run your code. However, if you are working on the Code:: Blocks, you can do this by clicking on the ‘Build and Run’ option.

Writing First C++ Program Important Points 

There are some important points you need to keep in mind when writing your code in C++ to ensure a smooth and error-free experience. 

  • At the beginning of your program, always include the necessary headers. You require “#include <iostream>” to perform simple input and output operations.
#include <iostream>
  • Every C++ program must have a main( ) function. It serves as the entry point for your program’s execution. Make sure to include `int main() in your code.
int main( ) {

    // Write your code here

}

  • Using namespace std; will enable you to use cout and cin without the ‘std::’ prefix, which will simplify your code.
#include <iostream>

using namespace std;

int main( ) {

    cout << “Hello, World!” << endl;

    return 0;

}

  • C++ is a case-sensitive programming language. Hence, make sure you use proper capitalization for keywords and identifiers.
  • Use proper indentation to make your code readable. It helps you and others understand the code structure easily. 
  • You must also use comments wherever necessary by enclosing your statement in the /*–*/ quote or just using the // before your statement.

Writing Your First C++ Program FAQs

How do I start writing the first C++ program?

You can start writing your first C++ program by following the steps below.

First, use a compiler to write your code. This is the first step before starting your C++ coding journey.
Now, write your first C++ program. You can reference this article to help you write your first C++ program.
Compile your program and run it.
Check the output on your screen.

However, refer to the article above to get guidance in writing your first C++ program.

Why is proper indentation essential in C++ programming?

The readability of C++ code depends heavily on proper indentation. Programmers and other users can easily understand the code structure thanks to indentation. Correct code alignment makes it visually obvious which statements are part of loops, conditions, or functions.

How do comments improve the readability of C++ code?

In C++, comments are used to add details about the code. They act as helpful explanations for programmers reading the code, even though the compiler ignores them.

Developers can better understand the purpose and reasoning of the code by adding comments, either with "//" for single-line comments or "/*-- */" for multi-line comments.

Also, read the related articles.

Recommended Reads
Java Vs C++ Vs Python Java or C++ which is better
What is STL in C++ Top Features of C++ programming language

 

Telegram Group Join Now
WhatsApp Channel Join Now
YouTube Channel Subscribe
Scroll to Top
close
counselling
Want to Enrol in PW Skills Courses
Connect with our experts to get a free counselling & get all your doubt cleared.