Variables are used to store information in your program. If you were writing a program to compute the sum of the first n integers, you would probably want a variable to keep track of how many numbers you had added so far, as well as a variable to keep track of your total so far.Variables in C have 'type.' In math, we have types of numbers like whole (counting) numbers, real numbers, rational numbers, and complex numbers.
- In C, variables can be:
- int: ...,-3,-2,-1,0,1,2,3,...
- float: -.5, 6.9999, 2.347, -3453453.5834668, etc.
- char: d, w, g, S, Y, E, [, $, @, or any of the digits (basically, anything you can type.)
- and more complex types we'll learn about later...
There are two steps you need to complete in order to use a variable correctly. The first one is 'declaration.' To declare a variable, you write its type, followed by the name you wish to give it. The second step is 'initialization.' You need to give a variable an initial value, before you can use it for anything else. Sometimes, you will want to initialize a variable right when you delcare it. Other times, you won't give a variable a value until later on in the program. Here are some examples:
int i; // declares a variable: i of type integer float x, y, z; // declares 3 variables of type float int n = 100; // declares variable n of type int and initializes it to 100 char p = 'q'; // tricky: p is the name of a character variable with value equal to the character q n = 25; // this assignment statement changes the value of n to 25Once you've created a variable, you can change its value or use it to compute some value or look at its value in a comparision statement, etc. Here are some basic examples:
n = a + b; // n gets the value of a + b i = 3 + (5*7); // i gets the value 38 n = i / 2; // n gets i divided by 2 (19 in this case) i++; // increment i (i = i + 1) if (n == 20) n = 5; // if n is equal to 20, change its value to 5Don't confuse = with ==. The single equal sign is used for assignment. The double equal sign is used to test equality.
One more note: a float is not equivalent to our definition of a real number. Computers only have a fixed amount of storage, so the number 1/3 will be stored as 0.33333333333 with some fixed number of 3's. This can lead to slight errors (just like calculators sometimes make) due to rounding or truncation.
Yet another note: Just as floats have their limitations, so do ints. There is only a fixed amount of memory allocated to any integer value. For this reason, there exists a maximum and a minimum value that an integer can have. The exact value will vary from system to system. (See K & R for more details.) Unlike the set of all natural numbers, which is infinite, the number of integers is finite. This can potentially cause problems in your programs. In assignment #3, you will encounter the "overflow problem." If you add 1 to the largest possible integer value, overflow occurs. The value you will get as a result will be the value of the least possible integer value. In a sense, the numbers wrap around to form a continuous cycle of values. In simpler terms: if you add (or multiply) really big numbers, you may get a negative number as a result. If the numbers are really big, you may get a positive number as a result (so just testing the sign of the result is not a sufficient way to detect overflow.) The positive number, however, will not be valid. Its value will be smaller than the numbers you were adding (or multiplying.)