Chapter III: Variables, Constants, and Enumerated Constants
In this chapter, we’ll touch down on how to store and manipulate data in your programs. First, however, I feel it’s important to explain a bit about memory.
When coders talk about memory, they are most often referring to RAM (random access memory). A long time ago, when memory was expensive, programmers had to write fast and memory-efficient programs. They had to be small, so as not to consume memory. However, most computers have at least 512 MB of RAM, if not more. The standard now is 1 GB, but that doesn’t mean you can just go crazy with your memory.
The basic building block of a program is a bit of memory. No, seriously, think of a bit as a single locker in a locker room. You can open this locker, place things inside of it, and then close it again. If you need to access something inside of that locker later, you can open it back up again to use whatever it contains.
Variables are how we store data in memory. Each variable type consumes a different amount of memory, as each variable type is typically used for different things. Some can be used to the same effect as others, but it’s important to know which to use in a section of your program. Below is a bit table. It will come in handy to know this information later, when you’re programming larger and more complex programs:
[code]1 bit = 1 bit
8 bits = 1 byte
1024 bits = 128 bytes = 1 kilobit
8192 bits = 1024 bytes = 8 kilobits = 1 kilobyte
1048576 bits = 131072 bytes = 1024 kilobits = 128 kilobytes = 1 megabit
8388608 bits = 1048576 bytes = 8192 kilobits = 1024 kilobytes = 8 megabits = 1 megabyte
1073741824 bits = 134217728 bytes = 1048576 kilobits = 131072 kilobytes = 1024 megabits = 128 megabytes = 1 gigabit
8589934592 bits = 1073741824 bytes = 8388608 kilobits = 1048576 kilobytes = 8192 megabits = 1024 megabytes = 8 gigabits = 1 gigabyte[/code]You don’t need to memorize this, although it never hurts to know about memory usage in your programs. Each bit is a locker in which a piece of information is stored. The locker itself is just a black box as far as we’re concerned. That’s the beauty of modern programming. You don’t need to know half of what your program is doing. You can just sit back and trust that it’s doing what you want. The only thing you need to concern yourself with is what YOU tell it to do.
Now let’s move on to the different variable types. Below you will find a table displaying the different variable types and their uses:
[code]int - A variable of type int (or integer). Typically used when working with whole numbers. Variables of type int consume 2 bytes of memory on most machines, but sometimes can consume 4 bytes of memory.
unsigned int - The same as type int, but only uses positive integer values. No negative values can be used when working with a variable of type unsigned int. Also consumes 2 bytes of memory (again, depends on the system).
long int - Same as an int, but consumes 4 bytes of memory instead of 2 bytes, allowing much larger values to be stored.
short int - A variable of type int defaults to short int on most compilers. Check your compiler’s manual for more information.
bool - Variables of type bool hold boolean values. That is, true or false values.
float - Variables of type float hold floating point number, or real numbers. That is, numbers with decimal places. This gives added precision to calculations, but consume more memory and can take longer to process on slower machines.
double - Variables of type double also hold floating point numbers, but use twice the memory as a float variable. This allows for larger values, but is far more inaccurate due to the size of the values you will typically be manipulating with this variable type. In most cases, it’s better to just stick to float for floating point numbers.[/code]The size of the various types can be different for different systems, so to check how much memory each type uses on your system, we’re gonna run through a nifty little program that will tell us just that. Using the sizeof function, we can find the size of pretty much any type out there. Let’s check it out: http://www.pastebin.ca/944567
A constant is a variable that never changes. This is useful when you need a variable that you can’t risk being unknowingly changed by another part of your program. This also allows for easy access of said variable throughout your program. For example, you could declare a constant for PI, as follows:
const float PI = 3.14159265Now you have a never-changing variable to hold PI.
Now let’s check out a sample program to illustrate constants at work: http://pastebin.ca/934918
Your compiler should flag an error (or errors), basically saying “hey, you can’t change a constant you idiot!”…
Am getting around to finishing this chapter… As always, if you have any questions, ASK!