## Monday, April 2, 2012

### Integers

Hello to all the little bots that live here in my blog. Today I'll be reviewing variables and data types.

As before, I'm no expert. I'm writing this down here to help me understand what I've learned so far.

So without furthor ado I give you variables:

Variable = A portion of memory that you use to store, retrieve and manipulate data.

Each variable I create has a type which represents what kind of variable is stored. So I know of 12 different types. I'm sure there are more, but these are the ones I know of so far.

Common Types                    Values

short int                                  -32,768   to   32, 767
unsigned short int                  0   to   65,535
int                                           -2,147,483,648   to    2,147,483,647
unsigned int                           0   to  4,294,967,295
long int                                  -2,147,483,648  to  2,147,483,647
unsigned long int                   0  to  4,294,967,295
float                                        3.4E +/- 38 (seven digits)
double                                    1.7E  +/-  308  (15 digits)
long double                            1.2E  +/-  4932 (19 digits)
char                                        256 character values
bool                                        true / false

*The range of values is based on your compiler. I'm using bloodshed dev C++

char is for storing character values, int for integers, float for single-precision floating point numbers, double for double-precision floating point numbers and bool for Boolean values (true/false).

Depending on what you declare is what you can store. Here's an example using all of the types I know of:

#include <iostream>

using namespace std;

int main()
{
short int level;
float nextLevel;
double distanceToTown;
char rideHorse;
bool haveHorse;

int orcsKilled, orcsUnconscious;

level = 2;
nextLevel = 3.5323;
distanceToTown = 72110.53423;
rideHorse = 'y';
haveHorse = true;
orcsKilled = 3500;
orcsUnconscious = 5400000;

cout << "\nLevel = " << level << endl;
cout << "Experience till next level = " << nextLevel << endl;
cout << "Distance to the next town = " << distanceToTown << endl;
cout << "Does the character ride a horse to town?\n" << rideHorse << endl;
// Don't display boolean values
cout << "How many orcs has the character killed?\n" << orcsKilled << endl;
cout << "How many orcs has the character knocked unconsious?\n" << orcsUnconscious << endl;

int food;
cout << "How much food do you wish to buy? ";
cin >> food;
cout << "You have bought " << food << " units of food." << endl;

typedef unsigned short int sshort;
sshort horses = 5;
cout << "\nYou have " << horses << " horses." << endl;

system("PAUSE");
return 0;

}

This little program displays:

Level = 2
Experience till next level = 3.5323
Distance to the next town = 72110.5
Does the character ride a horse to town?
y
How many orcs has the character killed?
3500
How many orcs has the character knocked unconsious?
5400000
How much food do you wish to buy ? (enter any number. I chose 5) 5
You have bought 5 units of food.

You have 5 horses

For Level I used a short integer because the number would not be that high.
For Experience I used a float because I wanted to use a fractional number
Same with distance to town, but used a double instead to create a larger number
I needed a character for Does the character ride a horse to town? so I used char
For how many orcs killed/unconsious I put the integer name on the same line. I could have put many more variable names on that line if I wanted. I don't have to go to a whole new line if I am creating a bunch of the same "types" of variables

A couple of key things to remember about variables:
• Always declare what type.
• Use the correct type of variable. Don't try to store a character value (the 256 characters) when declaring it's an integer.
• If you need fractional parts then use float or double
• You can declare the variable many different ways. You can declare it first without any value and later on give it a value.
• OR better yet, give it a value when you declare it
• identifier names (the name after you declared what type of variable it is) can be anything you want with a few exceptions.
• an identifier can only contain numbers, letters and underscores
• No C++ keywords
• Can't start with a number
•  Lastly, = (insert value) declares the value
Naming variables is pretty open, but try to follow a few rules. Be consistant, use descriptive names and follow traditions others have set. They don't become traditions for no reason. The way I'm handling naming is by capatilizing any second word. Some use underscores. For example:
int thisIsTheNameOfMyVariable = 5
vs
int this_is_the_name_of_my_variable = 5

I don't like hitting the underscore. It's a pain :D

I know I could go into really detailed explanation, but it's pretty basic.

Declare, Name, Value
• Declare your type of variable
• Name it
• Give it a value

Basic stuff.

int basicStuff = 5;
double notSoBasic = 5.5554;
char itIsBasic = 'y';
long double reallyNotBasic = 5.43432324525;
bool didYouDeclare = true;

All of those followed the rule of declare, name and value.

One last thing that is pretty cool. You can redifine how to declare a type with typedef:

typedef unsigned long ulong;

By using that I no longer have to type in unsigned long before declaring an unsigned long int. I can now just type ulong. Also you can just type short or long for short int or long int no need for typedef with those.

That's it for today. Before I go I would like to recommend again antiRTFM's utube videos and Beginning C++ Game Programming. I'm using both and am learning a lot.

Well, good night to all my little bots that thrive and live within my blog.

Same Rat-Channel.
Same Rat-Time.

#### 5 comments:

1. Bleep bloop!

I don't really know C/C++, but I don't think there are too many more primitive datatypes in it than the ones you've listed, actually. Things will start to get seriously interesting when you get to pointers.

This is a very different way of doing stuff than is done in most high-level languages these days, by the way—if you have something invisibly taking care of memory management and such, you don't think of a variable as a memory location at all, but simply a box to put something in. Also the distinction between variables and pointers disappears.

Carry on, this is interesting and I'm learning stuff too by looking over your shoulder. I've wanted to learn C++ properly for a quite a while, and this way I'm at least learning it vicariously as it were.

1. The box thing is pretty much how I think of it too. I can't wrap my head around memory location, but I can understand the size of a box to put things in :D

Glad I could be of help.

2. Memory locations are quite easy to understand, although they can be tedious to deal with. If you're writing C/C++, it's good to have some idea of what's happening down there. Here's the deal in brief.

As I'm sure you know, your computer stores data as bits, each of which corresponds to a transistor on your memory chip. The bits are wired together into blocks, usually of 8, i.e., bytes. Each byte has a unique address, known as a memory location.

So, when you write

int myLittleNumber = 5;

...you tell the computer "reserve 16 bits of memory at the location of your choice, write the value 00000000 00000101 at that location, and give me a pointer to it."

And boom, somewhere among your RAM sticks, a row of transistors assumes that state, and your CPU gets back the location that specifies exactly where.

And when you write

cout << myLittleNumber;

you're telling the computer

"Read two 16 bits from the location stored in myLittleNumber and output the result to the output buffer cout."

And again, boom, the CPU sends a flash of current down a set of wires, which reads the state of those 16 transistors you reserved earlier, and sends back the information by switching those same wires on or off in the same order.

So there's a direct, 1 to 1 relationship with the code you're writing, and current actually flowing in the innards of your computer. Pretty cool, eh?

And that concludes your free backgrounder for today. Bleep bloop!

3. Errata: it would've been more accurate to write "reserve 2 bytes..." and "read 2 bytes..." instead of talking of 16 bits here, because you can't address individual bits, only entire bytes. So when you're writing code, even machine-level code or something close to it like C, you're almost always thinking of bytes rather than bits, even though the stuff is ultimately stored as bits.

This, by the way, is why hardcore programmers like hexadecimal—two hexadecimal digits neatly represent one byte. So 0xff (hexadecimal) means 0b11111111 (binary), which is a much nicer representation of the physical state of the memory location than 0d255 (decimal).

(And what does a single hexadecimal digit represent, I hear you asking? A nybble, of course.)

4. And dang, that should've been short int, not int. I toldja, I don't really know C.