News Downloads Projects Documents/tutorials Staff

JE-SYSTEMS

Variables  


How variables works.

What is a Variable
Simple question, but it's actualy a little harder to define a variable. A variable is a 'name' which holds a value. You choose the name, and the value direct or indirect. Because the variable should hold a value, it need some memory to store this value. Theoreticly, we could have one type of variable, which could be defined like this:
var my_variable = 10;
And this should make a variable called 'my_variable' and give it the value 10. Now, this just work in the theory, it will not work practicly, cause the program needs to predefine how much memory the variable my_variable should use. We could then give my_variable very much memory, and then it should work. The problem with this is, perhaps we give it memory for holding numbers in size of 10^99. This would take up Very much memory, and it would be a hudge memory leakage, if the user only uses up to a value of 256 or something like that. Therefor we have...

Different types of variables
Different types of variables will take up different much memory. All variables holds a value, from 0 to 'endless' (theoreticly that is). What limits the different variables have depends how much memory is allocated for them. Here's a small list of common variables, and their minimum/maximum values:
  • char | 0<->255
  • int | -32767<->32767

    Here i want to add a small note. If we look at the int, it should (if we look at how much memory it uses) be able to keep values up to +65535, but one bit is used for what state the value is, if it's + or -, that makes the variable go to maximum +32767, but on the otherhand it can have the value of -32767, and doesn't have the lowest value of 0. If we try to go above +32767, then we will get a minus vaule back, because we have exceeded the length of the integer.

    The char, is normaly defined as char my_char = 'b', holds numbers as normal variables. The difference here is that the compiler/program will automaticly convert numbers to characters. For example perhaps 'a' is equal to 1, in that case my_char would have the value 1 if i places the character 'a' to it.

    Memory usage
    We have talked some about memory, and that the variables uses memory. Let's clear it out. All computers and chipsets are using a binary system, that means that values are 'written' like this: 00010010. Here's some more help about the binary system:

    16 8 4 2 1 - decimal system
     0  0 0 0 0 - binary system

    If we want to write the value of 2 in binary system, then it would look like this:
    16 8 4 2 1 - decimal system
     0  0 0 1 0 - binary system

    if we want the value 9 in the binary system it would look like this:
    16 8 4 2 1 - decimal system
     0  1 0 0 1 - binary system

    As you can see it's a pretty simple system. Now, we know that we use to count how much space something takes on a system in bytes. Byte isn't actualy the lowest unit. Bit is. One byte is equal to eight bits. And one bit is equal to one 0/1 in the binary system. so if we take the smallest variable (char which uses 1 byte), and split that up into bits, we get that a char using 8 bits, that means that the char actualy looks like this in the binary system: 00000000 (eight zeros). The biggest value a char can have is then 11111111 (eight 1:s). If we draw this up like we did before, we get this line:

    128 64 32 16 8 4 2 1 - in the decimal system
      1    1   1  1   1 1 1 1

    If we now adds a + between those decimal values: 128+64+32+16+8+4+2+1. Then we get the value of 255, which is the highest value a char can have. I want to drop a note here, most people say that a char can have the highest value of 256, an integer 32768, and so on, this isn't true, which we have learned today. If a variable should have the value 256 then we would have to use the very first of the 9th bit.

    You can count the variables maximum values with the formula (2^n)-1 (where n is how many bits the variable is using), It's here that missunderstanding of a char should have the highest value of 256, if you just calculate 2^8, then you will get 256, it's true a character can have 256 different values, with the 0, that means that the highest value is 255, which many people forgetts.

    What is a signed/unsigned variable?
    You can define a variable with signed/unsigned infront of the defination, like this:
    unsigned int my_int;

    Let's clear out what this does. Signed means that we take one bit of the variables memory, and use that to check if the variable holds a possitive or negative value. We talked earlier about an int, which goes from -32767 to 32767, this means that an integer is by default signed. If we know that we will not use negative numbers, we could define an int like this:
    unsigned int my_int;

    This will give my_int memory to have the highest value of 65535 (2^16-1), not that the lowest value my_int can have now is 0. Using signed/unsigned on your variables can sometimes save memory. Instead of changing to a variable type that takes up more memory, you can sometimes just change to signed or unsigned. Remember that every byte that is saved, is a good byte :)

    I hope you have found some help with clearing out how variables works. If you have any question about this tutorial, be sure to drop me a mail
  • Created to be compatible with Microsoft Internet Explorer at 800x600.
    Site designed by Jon Eriksson © JE-Systems, 2002