Program basics
C++ is a programming language, but what exactly is a program? A program is a set of instructions executed in sequence to give a desired output.
Let's look at our first program:
#include <iostream> // Program prints out "Hello, World" to screen int main() { std::cout<< "Hello, World."<<std::endl; return 0; }
We can look at this code line by line:
The hash (#) include is used when we want to include anything that is using valid C++ syntax. In this case, we are including a standard C++ library in our program. The file we want to include is then specified inside the angle brackets <>. Here, we are including a file called iostream.h. This file handles the input and output of data to the console/screen.
After the include, the double slash // is called a comment. Comments in code are not executed by the program. They are mainly to tell the person looking at the code what the code is currently doing. It is good practice to comment your code so that when you look at code you wrote a year ago, you will know what the code was doing then.
main() is a function. We will cover functions shortly, but a main function is the first function that is executed in a program, also called the entry point. A function is used to perform a certain task. Here, the printing of Hello, World is tasked to the main function. The contents that need to be executed must be enclosed in the curly brackets of the function. The int preceding the main keyword suggests that the function will return an integer. This is why we have returned 0 at the end of the main function, suggesting that the the program executed and the program can terminate without errors.
When we want to print out something to the console/screen, we use the std::cout (console out) C++ command to send something to the screen. Whatever we want to send out should precede and end with the output operator, <<. <<std::endl is an another C++ command, which specifies that it is the end of the line and nothing else should be printed on this line afterward. We have to use the prefix before the std:: code to tell C++ that we are using the standard namespace with the namespace std. But why are namespaces necessary? We need namespaces because anyone can declare a variable name with std. How would the compiler differentiate between the two types of std? For this, we have namespaces to differentiate between the two.
Note that the two lines of code we have written in the main function have a semicolon (;) at the end of each line. The semicolon tells the compiler that this is the end of the instructions for that line of code so that the program can stop reading when it gets to the semicolon and go to the next line of instruction. Consequently, it is important to add a semicolon at the end of each line of instruction as it is mandatory.
The two lines of code we wrote before can be written in one line as follows:
std::cout<< "Hello, World."<<std::endl;return 0;
Even though it is written in a single line, for the compiler, there are two instructions with both instructions ending with a semicolon.
The first instruction is to print out Hello, World to the console and the second instruction is to terminate the program without any errors.
It is a very common mistake to forget semicolons and it happens to beginners as well as experienced programmers every now and then. So it's good to keep this in mind, in case you encounter your first compiler errors.
Let's run this code in Visual Studio using the following steps:
- Open up Visual Studio and create a new project by going to File | New| Project.
- On the left-hand side select Visual C++ and then Other. For the Project Type, select Empty Project. Give this project a Name. Visual Studio automatically names the first project MyFirstProject. You can name it whatever you like.
- Select the Location that you want the project to be saved in:
- Once the project is created, in the Solution Explorer, right-click and select Add | New Item:
- Create a new .cpp file called Source file:
- Copy the code at the start of the section into the Source.cpp file.
- Now press the F5 key on the keyboard or press the Local Window Debugger button at the top of the window to run the application.
- You will have seen a popup of the console after you ran the program. To make the console stay so that we can see what is happening, add the following highlighted lines to the code:
#include <iostream>
#include <conio.h>
// Program prints out "Hello, World" to screen int main() { std::cout << "Hello, World." << std::endl;
_getch();
return 0; }
What _getch() does is it stalls the program and waits for a character input to the console without printing the character to the console. So, the program will wait for some input and then close the console.
To see what is printed to the console, we just add it for convenience. And to use this function, we need to include the conio.h header.
- When you run the project again, you will see the following output:
Now that we know how to run a basic program, let's look at the different data types that are included in C++.