Data and Variable Types

April 17, 2006

Hello friends,

Sorry, the posting have been light this week. I was unable to continue my studies fully because of my school. Well, anyway. This post will feature the incredible world of variables and some awesome things one can do with them. Continuing on from the previous post;

Variable Types

Before we learn how to declare variables and do cool stuff with them, we need to know their types and extent. I personally think this is a really important habit a programmer should have – declaring appropriate variables to suit your needs, that is – because although it only makes your program run 0.001 milliseconds faster, it is a really useful treat when you are maintaining your code later on. It is also useful because it helps other people, possible your colleagues, to understand what you are intending to do and what kind of data the program expects, so the possibility of a misunderstanding is diminished. Nobody would want to get a RGB value when the program is actually asking for a hex code.

So onto the types;

Variable Types

Yes, it is taken from one of my resources.

So, when you only want to deal with an integer, you should declare a variable that will hold an integer. An integer variable cannot hold anything else other than an integer. The compiler would probably give some fancy errors.

Declaration of Variables

In order to use variables in our program, we need to declare them. Plus, we need to declare them beforehand. So you cannot say “Hmm, I shall have a variable – but I’ll think of its type later on…” Nope. You must first say what kind of variable you want, and then assign it a name.

int oO;
bool hello;

Or you can declare more than one variable in one line, provided that they are all from the same type.

int a, b, w;

With this line, you have created 3 variables called a, b, w which can store integer type variables. Wow…

We have also seen that these variables can have one more additional feature. Signed, or unsigned. What this really means is that if it is a signed value, it can be either a positive, or a negative value. If it is, on the other hand, unsigned, it can only be a positive value.

When declaring variables, you do not need to tell the compiler whether the variable is signed or not. It will assume that the variable is signed. But still, if you want you can declare it like:

unsigned int a;

for your own convenience. A thing to look out for is that you can only use the signed or unsigned declaration when you are dealing with numbers. Char type would not accept this statement if you are not storing a number value in it.Now, for those of you who do know about programming, the variable scopes are more or less the same in C++. The variables that are declared outside of any apparent function are global variables; they can be accessed from every element within the code. Those variables which are declared inside a function are local variables; they are only valid for the function they are inside in. They cannot be referred from other functions, or from the main program flow. Of course, you could have skipped this, because I plan to touch on this when I have a little more knowledge about functions.

Lastly, we have, as a type of variable, the string. This type does not hold any characters, but it holds any combinations of alphanumeric values. “Hello this text can be the content of a string variable” can be the value for a string variable. It can also contain numbers and all kinds of stuff one can use in a text, but I wish to read further on this topic, because it seems a little different from other languages in C++.

Until next time,


One Response to “Data and Variable Types”

  1. u00deetta lu00edst mu00e9r u00e1. Heilbrigt fu00f3lk u00fearf ekki svona groddameu00f0feru00f0ir eins og stu00f3lpu00edpan er, heldur gu00f3u00f0an ma Click

Leave a Reply

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

You are commenting using your 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: