Part 2: Variables
When programming in C, something that you must always be thinking about are variables and constants. Variables and constants are the numbers that are used within the functions that allow you do anything within your program. Everything is a matter of numbers. Even characters of text are actually referred to by numbers inside the computer.
Computers speak in the language of binary. This is also called digital information, as opposed to analog. The only things it understands are 1's and 0's. On and off. A bit is a single binary digit. 1 or 0. These are the only numbers you will see in binary, also called base 2. A nibble is 4 binary digits put together. Each digit has a value to the power of 2 according to its position. For example, 0101 is equal to the decimal, or base 10, number 5. The first digit is the number of 2^3, or 8. If there is a 1 in that position, then it is equivalent to decimal 8. The next digit is for 2^2, or 4. In this case, the number has a 4 in it. The next digit is 2^1, or 2 and the last digit is 2^0 or 1. Add the results and you have 0 (0 * 2^3) + 4 (1 * 2^2) + 0 (0 * 2^1) + 1 (2^0) = 5
Some computer related symbols would be handy to go over right now. The symbol ^ is the computer equivalent of exponentiation. So 2^3 is the same as 2 to the power of 3, or 2 cubed. While this helps to understand the notation in the previous paragraph, this will not work in C programming. The asterisk, *, is the symbol for multiplication since it would be too confusing to use the letter x or X. And the symbol / is used for division, for example 1/2 is 1 divided by 2, or 0.5. The addition and subtraction symbols are obvious, + and -.
A byte is 8 bits, for example, 0110 1101. In binary this number is the same as the decimal number 109. For easy conversion between decimal and binary, and for ease of display, especially as the numbers get larger, hexadecimal, or base 16, is most commonly used. Each nibble corresponds to a single hex digit, and a byte would be 2 hex digits. The previous example would be written as 6D in hexadecimal. D in hexadecimal is the same as the decimal number 13. Since there are no single digit numbers for 10 though 15, the first 6 letters of the alphabet are used. 15 in hexadecimal is F, 16 is 10 (1*16^1 plus 0)
Since humans don't easily speak and understand binary, you write programs in language's you can understand. C is an example of a language written in something much easier than binary. Then the language can be compiled, like C, by a compiler, creating a binary file that the computer can use and understand. Also possible are interpreted languages, like BASIC, which the computer reads in one line at a time and converts to binary in real time. This can mean a major loss in speed and is one of the largest advantages for compiled languages over interpreted ones.
You must declare any variables or constants before you can use them. You will need to know what you are going to use the number for before you can declare it.
Constants are used when you want a number to be just that, constant. It will not change through the running of the program. You declare a constant with the keyword "const". This tells the compiler that the value stored in that constant will not change.
const int my1stconstant = 122;
The name of the values, called the identifier, can be almost anything you want, as long as it starts with a letter, has no spaces in it, and is not the same as a predefined C keyword like "const". A good notation concept is called "Hungarian Notation," and put simply, the identifier name explains what the identifer is, in addition to what it is used for. When you declare them, you can assign them a value immediately with the assignment operator, the equal sign. This is not required, but if you know the starting value, it saves time and space to define it when you declare it.
Another way to declare and define a constant is to not use a identifier at all but to just use the number itself in the program. The number 122 in the above line is an example of that.
Variables are just that, variable. They will change through out the running of the program. They can be anything from a counter that counts from 1 to 1000 or a variable to keep track of a game's score, which will change every time points are gained. There are different types of variables for different value numbers.
The smallest is the "char" short for character. This is generally a number ranging from
-128 to 127. In binary, this is the same size as one byte. There is also an "unsigned char"
which does not have negative values so can go from 0 to 255. This is almost always used
only for text. To store text in C you use numbers. For most computers, each letter has
a number value based on
char cMyCharacter = 'B'; unsigned char cMyOtherCharacter = 'b';
Now cMyCharacter holds the number value of 66 and cMyOtherCharacter holds 98. They can be used to refer to the number or to the equivalent letter. Notice that I used the single quote ( ' ) instead of double quotes ( " ) to delimit the chatacters. When dealing with characters, the single quote is used to denote a character constant relating directly to the ASCII value of that single character. Double quotes are used for string contstants and add a null character (value 0) to the end of the string. This will be covered in an additional article on strings.
Next is "short" or "short int". It is usually made up of 16 bits or 2 bytes or 4 hex digits. This is sometimes referred to as a "word". It can hold numbers from -32768 to 32767. An "unsigned short" can hold numbers from 0 to 65535. Since most computer processors are 32 bit now, this type is rarely used anymore.
Short for integer, "int" is the most commonly used type of variable when working with numbers. For most compilers it is the same as a "long" or a "long int". Check your compiler manual to be sure. This means it is made up of a long word, containing 8 hex digits, 4 bytes, or 32 bits. It can hold numbers from -2147483648 to 2147483647. An "unsigned int" or "unsigned long" can hold numbers from 0 to 4294967295.
All these types of variables have been for use with integers; numbers without a fraction
or partial number. For using fractions or decimals you have to use a different type. It is
called a float for floating point, referring to the decimal point. This also uses 32 bits
for holding information, but since part of that space is used to hold the position of the
decimal it only has about 6 places of precision. This may be different on your computer.
Look in your float.h file for the actual number. This means if the number is
1.234567, the 7 is not guaranteed to be accurate. A float can hold numbers from
+- 3.40*10^-38 to +-3.40*10^38. This may also vary. Check your float.h for the
exact numbers.
If you want more precision you can use "double". A double uses 64 bits, 16 hex digits, or
8 bytes. It has a precision up to about 15 places. Again see float.h for exact
numbers. Using a double when you do not need that much precision is a waste of memory since
it generally uses twice as much space to store a double as it does to store a float. It can
hold numbers from +-1.79*10^-308 to +-1.79*10^308.
Also available is a "long double" but you do not need to go into that at this point. If you really think you need to use a "long double" you can check your include files and compiler manual for the specifics it offers.
You will notice that I used the words "usually" and "generally" when giving many of the values. This is because these settings can be different depending on the computer you are using. A word is usually defined as the largest block of information stored in memory that a computer's CPU can handle at one time. For 16bit CPUs like the Intel 80286, this is a 16bit block. However, even though the Motorola 68k series of processors has always handled 32bit blocks of memory, they have defined a word as being 16bits, a long word as 32bits and a quad word as 64bits.
Check your C compiler include file limit.h for the actual values set for each
variable type to be certain of their limits before using them. You can always assume,
however, that a short is less than or equal to a int which is less than or equal to a long.

