C++ is used in various domains, such as embedded software, real-time operating systems, game development, and finance, and because it supports both procedural and object-oriented programming styles, it is both strong and versatile.
In this article, we are going to discuss the basic structure of a C++ program and show you how to write a simple “Hello World” program.
C++ Program Structure
Before we write the “Hello World” program in C++, let us first discuss the primary elements of a C++ program. Here is an example of a C++ program skeleton:
Because every C++ program adheres to this basic structure, we will now explain the primary elements of this structure in depth.
The first line is “#include <iostream>”. Here, “iostream” stands for input/output stream, where a stream is a series of characters or bytes. This line instructs the preprocessor to include the content of the library in the program.
There are several libraries available in the C++ programming language. Libraries contain built-in objects and functions that programmers can use to write programs, and they are provided by the C++ compiler. When we install the C++ compiler, we get all the associated libraries.
The “iostream” includes the following objects:
- cin: the standard input stream
- cout: the standard output stream
- cerr: the standard output stream for errors
- clog: the output stream for logging
Every C++ program has a “main()” function. In this example, the value returned by the main function is an integer. Therefore, after the “main()” function is run here, a value of 0 will be returned.
The opening curly brace indicates the beginning of the body of the main function. The closing curly brace indicates the end of the body of the “main()” function. The rest of your code will be placed inside the curly braces
Hello World (HelloWorld.cpp)
Now, let us write a simple “Hello World” program and execute it. We will use the C++ standard library stream resources to write the string “Hello World” to the standard output.
int main()
{
std::count << ”Hello World” << std::endl;
return 0;
}
To compile the C++ program, you need to use the command g++ <filename> -o <output>.
We discussed the “iostream” header file in the previous section; “cin” and “cout” are commonly used objects: “cin” is mainly used to get input from the keyboard and store the data as a variable, while “cout” is used to print the data on the screen.
We can use “cout” to display “Hello World” to the screen. However, we cannot use the “cout” object directly since it belongs to “std” namespace. Therefore, we use the scope resolution operator (i.e., ::). In addition, to print a new line, we used “std::endl”.
If you don’t use the scope resolution operator, you will get the following error:
int main()
{
count << ”Hello World” << endl;
return 0;
}
To fix the above error, you can either add the scope resolution operator correctly or you can mention the namespace at the beginning of the program. If you want to use “cout” without using the scope resolution operator, then you could write the following code:
using namespace std;
int main()
{
count << ”Hello World” << endl;
return 0;
}
In the above program, we mentioned the “std” namespace in the second line (i.e., “using namespace std;”). Therefore, we do not need to use the scope resolution operator every time we use an object from the “std” namespace, and we can simply use “cout” to print something to the standard output instead of writing “std::cout”. Similarly, we do not need to use the scope resolution operator for “endl”.
Now, we will compile this program and see the output:
As you can see, we get the same output.
Conclusion
C++ is a flexible, general-purpose programming language that is widely used in various domains. It is an extension of the C programming language and it inherits the syntax of C programming. In this article, we showed you how to write a simple “Hello World” program in the C++ programming language and explained various elements of the program.