Your First C++ Program

Writing first lines of code is always interesting. In this article, we are going to write our very first C++ program. This is a simple ‘hello world’ program, that will provide you some insights regarding the syntax and basic structure of the language. So, let’s write our first C++ program. Open a text-editor. Write the following lines.

//first C++ program
#include <iostream>
//header file included
using namespace std;
//main function
int main(){
    //prints hello world
    cout<<”Hello World”<<endl;
    return 0;
}

Save the file as helloWorld.cpp. You should remember the directory, that you are storing the program into. Now, move into that directory and compile the program. If you are using an IDE, that the IDE takes care of the directory and compilation. But you need to know the operations required for the particular IDE. I am using Linux to compile and run the program using the terminal. So further steps will be done on a terminal. You can use your own settings for compiling and running the program.

Now, to compile the program if you are using a command line in Linux OS you need to type

g++  helloWorld.cpp -o helloWorld

Now, if the compilation is successful, you will get a helloWorld file. In the linux terminal type to run the program

./helloWorld

If you are using an IDE, then you need to know the commands for an IDE for its compilation and running the program.

The program runs and output of the program is

Hello World

Hope, that you have got the output of the program. Its time to decode the program, so let us understand the program step by step

Saving as .cpp

Save all of your C++ programs with extension .cpp, .c, or .cp. You need to the save the file as any of the above extension for compiling the program. We will be saving it as .cpp so it is better that you do the same.

Commenting your code

Use // for commenting the code. Any line starting with // is a line comment. For commenting multiple lines we should use /*…*/.

/* The comment begins from here
It continues until a slash and a * is used
So it lies till here. */

Commenting is an important part of the program. If people are working in a team, commenting helps to understand the code better for the team mates. Commenting is also beneficial to recall the working of the code, if the code is viewed after some time. It is a good practice to comment the code, although not commenting the code does not cause harm.

#include

All of the C++ programs start with this line. It is a preprocessor directive. It tells the program to include the file. In this case it tells the compiler to include the standard iostream file which contains the standard input/output functions. Other than iostream, there are many files which can be included using the directives. It helps to code faster, when the proper file is included for a particular functionality. In case if #include<iostream> does not work try using #include<iostream.h>. This problem can occur if you are using an old compiler.

using namespace std;

This is using directive. It is a new feature designed to simplify the process of programming. If you do not include this line, you will need to replace cout with std::cout in the above program. You will get to know the functionality as we write different programs.

int main()

This is the main function of the C++ program. All the other functions work for main function to accomplish the particular task, required by main. Every C++ program must include main function. You can’t use any other type of main as Main() or MAIN(). It has to be main(), else you will get a compile time error. The main function prefixes a data type. In this case it is ‘int’. This data type returns a particular value at the end of the program, depending on the output required.

{ and }

These are the opening and closing braces. These are included at the start and the end of the function or a block. In this program, main is function, so the code of main follows in { and }.

cout<<”Hello World”<<endl;

This lines tells the program to print the line “Hello World”. A semi-colon is used to end the statement. In fact, every line that ends with a semi-colon is a statement. cout is used to output the characters and display it on the screen. << are used with cout to provide the information. endl is a special notation that represents beginning of the new line. The cursor moves in the next line whenever cout encounters endl. So this line prints “Hello World” and the cursor moves on the next line.

return 0;

The return statement returns the value from the function. If you do not use return 0 statement in C++ programs, it is fine for some compilers, but some compilers creates problem. So, it is a safe option to use this statement.

The above mentioned are some of the keywords used for programming in C++. There are many others. You will learn those keywords as you progress further. All of those forms the basis of programming and helps in better programming.

After your first program you should now be able to write some of the simple programs, that outputs the value you desire. Our next milestone, will be to know the data types in programming. Then, we can play with values and not just text.

Happy Coding

Leave a Reply