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…

Advertisements

Hello World!

cout << “Hello World!” << endl;

Hello there,

I am Bilal Ahmad Cheema and this blog is all about programming! Learn C++, create tiny programs and if you encounter some errors, ask me and the community for a fix. Its about innovative programs which are made using some easily understandable colorful logic! It’s about the game that we all want to develop! It’s about the algorithms that feels hard although, are very easy.

It is the very first post of this blog. There is not only C++ but there is Java as well. For C++, there will be programs related to SDL2, Box2D, OpenGL and Bullet. For Java, there will be programs related to standard JavaFX only. I am not an expert programmer. Therefore, I am not a magician. But I am interested in programming. To know more about me, check out My Story.

Feel free to ask! Its about sharing and caring!