C++ Programming Series: Hello World and Variables

If you already read the previous post and installed Microsoft Visual Studio Express Edition, you can just open it and somewhere, you will see New Solution. Just set a new one and add an empty cpp or source file to it. In this cpp file, copy and paste that code below to it and then, run it. If it runs fine, it will output, “Hello World”, on the console screen.

Here’s the code for printing, “Hello World”:

#include <iostream>
using namespace std;
int main()
 cout << "Hello World" << endl;
 return 0;

The word cout may be taken as a short form of ‘console output’ and endl as ‘end the line’ or put an enter to it. << is an operator called as ‘put to’ operator. Lines of code present between two curly brackets may be taken as a block. There are two statements in the block. Each statement is required to be finished by a semi-colon. main() is the place where codes will run (or execute). For now, this is enough, you will understand the whole of it later on. Don’t worry about this! It looks hard but is very simple.

You can also print numbers and they can be typed in without double commas as well. Just replace “Hello World” with some integer.

cout << 221133 << endl;

Integers are any numbers ranging from infinite negative to infinite positive numbers. But what if we want to store an integer? I mean we may need to call it many times and manipulate its value as well which is hard with a constant like the above one.

To do that, we use variables. To store an integer say 221133 in a variable called as x and then call it later on, we can do something like this:

int x = 221133;
cout << x << endl;

Note that word int. It is the short form of a word called integer. A variable must be written with its type. int or integer is one of them. The cout then, treats x as 221133 which is of type int. Format of any variable is given as:

[type] [name] = [value];

You can declare a variable first and then, give a value to it (initialize it). If you don’t give it a value and print it, you may get an error or most probably, a random value. Therefore, always initialize the variables before using them.

int x;
x = 221133;
cout << x << endl;

The most important thing about the variables is that they have a specific scope. From scope, we mean the area or space where the variable can be used. The scope of a variable is the remaining block after its declaration. If the variable x, is declared in the block below main(), its scope is that block i.e it cannot be used above the declaration or outside the block’s ending flower bracket.

Don’t get panic with all the things here! There is way much more stuff. Try to figure out your learning rate and proceed according to it! Just try to type codes and make them run…


2 thoughts on “C++ Programming Series: Hello World and Variables

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s