Site icon TestingDocs.com

C++ Hello World Program

Introduction

In this post, we will write a small C++ Hello World program and discuss the general structure of a C++ program. In C++ the executable unit is called a function. We will use Code:: Blocks IDE to create a C++ project.

Launch IDE

Launch Code:: Blocks IDE.

https://www.testingdocs.com/launch-codeblocks-ide-on-windows/

Create a Project.

https://www.testingdocs.com/create-new-project-in-codeblocks-ide/

C++ source file

Add C++ source file to the project.

https://www.testingdocs.com/add-c-source-files-in-codeblocks-ide/

Add C++ Code

Let’s write source code for the program.

// Hello World C++ program
#include<iostream>
using namespace std;
int main(){
    cout << "Hello World!" << endl;
return 0;
}

 

The first line is a C++ comment. Comments are ignored by the compiler. They are used to describe the operation of the program.

#include<iostream>

This loads the header file. The include directive instructs the pre-processor to copy the contents of the specified file into the program. The iostream library performs input and output. These are called pre-processor directives. 

using namespace std;

Loads the namespace std. This tells the program will be using objects that are named in a region called std. Namespaces are used to separate sections of the source code.

main()

The main() routine is the start of every C++ program. It is like the entry point where the C++ program execution starts.

cout

cout is the object that writes to the standard stdout device, i.e. the Console window. It is defined in the iostream header file.

endl

endl is the C++ newline character.

<< is used to pass characters from the right to the object on the left. This operator is an insertion operator.

Run the program

Build and Run the program to see the output as shown in the picture.

 

 

The return statement returns an integer value to the operating system after the C++ program completion. It is a convention that a zero result from the main indicates that the program ran successfully without any errors.

That’s it. We have successfully run a simple Hello World C++ program.

Code::Blocks Tutorials

Code::Blocks Tutorials on this website can be found at:

https://www.testingdocs.com/code-blocks-tutorials/

For more information on Code::Blocks IDE, visit the official website:

https://www.codeblocks.org/

Exit mobile version