Variables

A computer program uses variables to store the data it needs during execution. The values of variables are stored in the computer's memory. Variables are also given a name, so that they can be referred to in the computer program.

You can think of a variable as a box into which you can put a small amount of information. Different types of boxes can store different amounts and different types of information. All the boxes you create are stored in the computer's memory. Each box is given a name, so you can get the information from the right box.

In C, there are different types of variables for different types of data. Integer numbers, real numbers and character data each have their own data type. These data types are summarized on the next page. For now, we will focus on the variable type int, which is used to store integer data.

In order to use a variable, you must first declare it. This tells the compiler to make a box in memory of a certain type and give it the name you specify. The general form of declaring a variable is as follows:


	<variable type> <variable name>;
For example, to create a variable called a of type integer, you would use the following command:
	int a;
You can also declare several variables at once by separating the names with commas. For instance:
	/*create three integer variables called a, b and c*/
	int a,b,c;

Variables names should indicate the nature of the data they are storing. Descriptive names will make it easier for people (including you!) to read and understand your code. The variables a, b and c are not good names as they are not descriptive. Better names might be studentAge and birthYear.

	int studentAge, birthYear;

Only certain characters can be used in variable names. A variable name can start with either an underscore character or a letter (small case or capital). The rest of the variable name can be made up of numbers, characters or underscores. Variable names are case sensitive, so the same word can with different cases can be used for two different variables (in practice, this is confusing and should be avoided). The following shows some examples of valid and invalid variable names:

Valid variable names:
int firstNumber;
int FirstNumber;
int number21;
int student_age;

Invalid variable names:
int 2number;  /*variable names cannot start with numbers*/
int first number; /*variable names cannot include spaces*/
int int; /*variable names cannot be reserved words,
		such as type names or commands in C*/
If you use an invalid variable name, you will get a compilation error.

In C, variables must be declared at the start of a function. For instance:

int main(int argc, char* argv[])
{
	/*declare all the variables for this function*/
	int u;
	int studentAge, courseNum;
	int year;

	/*now include all the commands that are used in the function*/
	printf("Hello World!\n");

	return 0;
}
It can be useful to add comments when you declare a variable to describe what it will be used for. This will make your code more readable.

We will now look at different variable types.

next


 home                      table of contents                      about