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
maybe taken as a short form of ‘console output’ and endl
as ‘end the line’ or put a new line to it. <<
is an operator called ‘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 << 786 << 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 786 in a variable called x and then call it, later on, we can do something like this:
int x = 786;
cout << x << endl;
Note that word int
. It is the short form of a word called an integer. A variable must be written with its type. int
or integer is one of them. The cout
then treats x
as 786 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 = 786;
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 about 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 responses to “C++ Programming Series: Hello World and Variables”
[…] to that function. If you don’t know anything about the scope of a variable, you should read this post […]
[…] destroyed and can not be used anymore. This is perhaps, what we call the scope of a variable. The very first post of this series might remind you what I meant […]