Variables

A variable is a place in a computer's memory (RAM) where a value can be stored. The name is drawn from the algebraic concept of using letters in equations to indicate a place where any numeric value can be stored.

In C, there are many types of values. Numbers, letters, strings of letters, memory addresses (pointers), complex user-defined structures, etc.

A variable has a name and a datatype. The datatype determines the size of the storage location. A variable also has a scope, which determines the lifespan of the variable and what parts of the program can access the variable by name.

Let's start out with an example:

main()
{
	int radius;
	float area;
	float pi = 3.14159;

	printf("Enter the radius: ");
	scanf("%d", &radius);

	area = pi * radius * radius;

	printf("The area is %f\n", area);
}

There is actually a lot to be learned from this example.

Initializing Variables

At the beginning of our function main(), we have declared three variables: radius, area, and pi. These obviously corespond to the radius and area of a circle, and the geometric constant pi. The variable we called radius has a datatype of int, which means it can store an integer (a positive or negative whole number). The variable called area has a datatype of float, which means it can store a floating point decimal number. The variable called pi also has a datatype of float.

We can declare variables inside any function, but they must be done at the top of the function. ie, we declare our variables first, and then we can write executable statements that operate on those variables.

It is important to understand that any variable has an initial value. In our example above, we have told the C compiler what we want for an initial value for the pi variable (3.14159). But we have not done that for our other two variables. Do they have an initial value? Yes. What are their initial values? Nobody knows. You should assume they have completely random values, and they usually do.

When a variable is created (or instantiated, to use OOP jargon), a portion of memory is set aside to store values for that variable. But if you do not initialize the variable, then the value will be whatever happened to be stored in that memory before it was allocated for the variable. The variable is getting memory that may have already been used, and its initial value is unpredictable. For this reason, if you do not initialize a variable when you declare it, then you should be sure you store a value into the variable before you try to use it.

Some examples of variables declared with initial values:

	int x = 0;
	char ch = 'C';
	float y = 8.1;
	char s[7] = "String";
	char *str = "This is a string";
	int z[3] = { 1, 2, 3 };

Assignment Statements

An assignment statement in C assigns a value to a variable. An assignment statement has the format:

variable = expression;

The name of the variable being modified is specified on the left hand side of the equal sign. The expression on the right can be a constant value, an algebraic equation, the return value of a function, etc. Some examples of assignment statements:

	x = 2;
	y = x + 2;
	area = pi * radius * radius;
	ch = getchar();

In addition to the equal sign (=), there are more advanced assignment operators, which will be described later.

Combination Assignments

C has an interesting capability, the ability to combine assignment statements:

	x = y = z = 0;

The statement above stores the value 0 in three variables (x, y, and z). In theory, this is because all assignment statements also evaluate as an expression. The expression:

	0

Has a value of 0 (obviously). The statement:

	z = 0

stores the value 0 into the variable z, but the whole assignment statement has the same value as was stored in z. We can take advantage of that by storing that value into another variable:

	y = z = 0;

That statement too has a value, and could be assigned to yet another variable. We could nest assignment statements together almost infinitely, if we wanted.

Naming Variables

We've said that all variables have names. There are rules that the programmer must follow when naming variables (and functions, etc).

Programmers should use descriptive variable names whenever possible, so as to make your program more readable. With the original BASIC programming language, only the first two characters of variable names were significant, so one and two letter variable names were very common. If you are using a variable to store the area of a circle, name your variable area or CircleArea or something.

Fielding an Input Value: scanf

While you are learning to program, you will need to be able to take user input (from the keyboard) into variables in your programs. Later, you will learn more advanced (and safer) methods of doing that, we will introduce you to the quick and dirty method: scanf.

Displaying a Variable's Value: printf

Arithmetic Operators

Increment and Decrement

Here is another C peculiarity which exists because of C's closeness to the underlying machine language. Must CPUs have an increment and decrement instruction. C does as well, the ++ and -- operators.

	x++;		/* Add 1 to x */
	y--;		/* Subtract 1 from y */
	a = b++;	/* Assign a the value of b, then add 1 to b */
	c = ++d;	/* Add 1 to d, then assign c the new value */

++ and -- are special assignment operators. The ++ operator adds 1 to the value of the variable and stores that new value back into the variable. The -- operator subtracts 1. They can (and often are) use as standalone statements.

The last two examples above illustrate that the order of the ++ or -- operator in relation to the variable it is modifying is important. If the ++ or -- operator appears before the variable, then the value of that expression returns the new value. If the operator appears after the variable, the expression returns the original value, before the increment or decrement operation.

Bitwise Operations

CPUs have instructions that perform "bitwise" operations on numbers. C has operators that do the same thing. The usefulness of these may not be entirely obvious to a new programmer, but as time goes on you will find yourself using them frequently.

	x = a & b;		/* Bitwise AND */
	y = c | d;		/* Bitwise OR */
	z = e ^ f;		/* Bitwise XOR */
	g = h << 1;		/* left shift */
	i = j >> 2;		/* right shift */
	p = ~q;			/* Bitwise NOT */