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:
using namespace std;
short int level;
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;
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;
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?
How many orcs has the character killed?
How many orcs has the character knocked unconsious?
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
int thisIsTheNameOfMyVariable = 5
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
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.