A variable is a storage place in the memory, set to store different kinds of
values. Before I show you how they work, let me explain the different kinds
of variables there is. First, lets just put up a list with all the variable
types:
C++
|
Description |
Range
|
bool |
Boolean value |
true or false |
char |
One-byte integer used to hold a character |
-128 to 127 or 0 to 255 |
unsigned char |
One-byte unsigned integer |
0 to 255 |
signed char |
One-byte integer |
-128 to 127 |
int |
Integer of “natural” size,(either two or
four bytes) |
-32.768 to 32.767 (two byte)
Approx. -2.1bill to 2.1bill (four byte) |
unsigned int |
Unsigned integer |
0 to 65.535 |
short |
Two-byte integer |
-32.768 to 32.767 |
unsigned short |
Two-byte unsigned integer |
0 to 65.535 |
long |
Four-byte integer |
Approx. -2.1bill to 2.1bill |
unsigned long |
Four-byte unsigned integer |
Approx. 0 to 4 billion |
long long |
64-bit integer |
+-; 2 to the 63rd power |
float |
Single-precision floating point |
+-; 3.4 times 10e38 |
double |
Double-precision floating point |
+-; 1.8 times 10e308 |
long double |
Extra wide floating point |
At least as large as double |
size_t |
Holds sizeof return value |
Same as unsigned short |
wchar_t |
Wide character, for use with
international character sets |
Same as unsigned short |
wint_t |
Holds wchar_t value of WEOF |
Same as long |
|
|
|
You might want to print that table and have it in a handy place.
As you can see, there are many different variables for different types of
storage. Say that you wanted the user to type in his age, and you would like
to store that number in memory for later use. You would then have to think
of the appropriate variable to store it in. So let’s get a number variable,
right? As you can see by the list above, there are many to choose from, so
which one should you choose? Always consider that you might want to reuse
functions of the program you are making, and that the variables might get
higher than in this certain case. The integer value seems to fit in this
case, since no one gets older than 32.000 years (though some may look
older). This function could later be used to store other peoples age as
well. Say that we where going to make a bank account variable, that stores
the amount of money you have in the bank. In my case, a string variable
saying ‘bankrupt’ would be more than enough, but let’s just say that I have
5.000 on my account. I could of course use an integer variable to store the
amount, since it’s between -32.000 to 32.000. But say that I suddenly were
to win the lottery and I got heaps of cash? Then I would have to go back and
change the variable type. Not that hard, you might think. Well, what if the
application was gigantic, and that you had to take down the system in the
bank it was running for a certain amount of time... bad idea. In other
words, think a bit ahead!
“Damn this guy is stupid! I have figured it all out! Why doesn’t he just use
the largest variable type, then he wouldn’t have to think about it any
more?” The question is easy to answer; Memory. This is especially important
when programming for consoles, which doesn’t really have that much memory.
Consider these types two numbers:
100.300.004.030.090
130
The first number is clearly bigger than the first. This is also the case in
memory. The first number takes up more memory space than the last. Since you
cannot save the first number in an integer value (it is bigger than 2.1
billion – look at the table again), you have to find another variable to
store it in. For now, let’s say float is a nice variable to use. We would
then call the variable bigNumber. The variable bigNumber is made like this:
float bigNumber;
First comes the variable type (in this case float), then the name that we
want to call this variable. I though bigNumber would fit, but variables can
be named whatever you like. Beware though, you cannot call them the same as
you might call for example a function. Naming a function and a variable the
same name would be like naming two of your kids the same name. It would only
get confusing - and so it is for the compiler, which would yield an error.
Anyway, this sentence is called a “statement”, and as every statement, they
should end with a semicolon. Here are a few more rules about variable
naming:
Cannot start with a number (example “123abc”)
Cannot start with an underscroll (example “_bigNumber”)
Can contain numbers and symbols if not the first character
Always name variables for easy understanding and reference (myAge == good
practice, numberVariable633 == bad practice)
As you can see, we broke none of these rules when we made the bigNumber
variable. What we didn’t do was to assign it the number it should store.
This can be done like this:
bigNumber = 100.300.004.030.090;
All calculations go on the right side, and the result is then transferred to
the variable on the left. In this case, there is really no calculation, but
still the end result is transferred to the variable. So, to make a variable
and then assign it a value can be done it two steps, as seen above. We can
also do it in one nifty little statement, as done here:
float bigNumber = 100.300.004.030.090;
This saves some typing, and is a whole lot easier to read.
Why don’t we store the 130 number in a float value as well? A float value
takes up a lot of storage space in memory, so we’ll have to find another
variable to store it in. If you look at the chart above, you’ll see that an
“unsigned int” looks like a good variable to use. We’ll call it smallNumber,
and it can be declared like this:
Unsigned int smallNumber = 130;
This concludes the first lesson about variables. You probably have a lot of
questions, but try not to think too much in detail about the variables.
Think about them as simple storage places, which hold numbers. I'll cover
character and string (sentence) storage in the next part of this article.
|