The first task to be performed when writing any program that manipulates numbers is to decide where to store them and tell the computer about it. Numbers are stored in the computer's memory. Individual memory locations are known to the computer by their reference number or address but programmers, being human beings, much prefer to give the locations names that can be remembered easily. It is the compiler's task to manage the relationship between the programmer chosen names of memory locations and the internal reference numbers. This is done using a directory or look-up table. A programmer is not normally in the least bit interested in the internal reference numbers although, as we shall see in a later chapter, a C programmer can determine and display these reference numbers or addresses should she or he want to. The important thing about computer memory is simply that numbers, once stored in a memory location, stay there until either the program finishes or some other number is stored in that location or the computer is switched off.
The parts of a program that tell the compiler that memory locations are required and should be referred to by certain names are known as declarations. In its simplest form a declaration consists of a word describing what type of memory is required followed by the names to be used to refer to the locations.
For example line 6 of the sum of two numbers program reads
int x,y; This requests that the compiler reserves two memory locations of type int and that they can be referred to as x and y .
At this stage in our study of the C programming language, we will only consider one type of memory location known as int . Memory locations of type int are used to store integers. There are many other types and variations which will be considered in a later chapter.
The word int is special for the compiler. Whenever the compiler sees the word int in a source program, the compiler assumes that it is processing the start of a declaration which requests the reservation of one or more memory locations of type int . In computer jargon we say that int is a keyword or reserved word. Most programming languages have keywords and misuse of keywords can cause obscure and puzzling error messages. A complete list of C programming language keywords will be found at the end of this chapter. The meaning of them will be made clear in due course, but, in the mean time, you must avoid using any of these words unless you know what they mean.
The word int is followed by a list of names to be used. The names in the list are separated by commas. As in all other programming languages there are rules for acceptable names of memory locations. The rules for the C programming language are
You cannot declare the same name more than once. The name of a memory location must not clash with the name of a function, i.e. you can't have memory locations called main and printf in a program that uses these functions. All declarations are terminated by a semi-colon. Declarations may be spread over several lines if you wish. This is shown below.
int x, /* first number */ y; /* second number */Multi-line declarations are probably preferable to declaring several names on a single line because they allow descriptive comments to be placed after each declaration. The following common alternative form is simply two declarations.
int x; /* first number */ int y; /* second number */
Memory locations used for holding numbers in this way are often referred to as variables because it is always possible to change the number stored in the location. The names associated with memory locations are known as identifiers. It is common practice to talk about the variable x , this is, occasionally, misleading because what is really meant is the number stored in the location whose address is x . The circumlocution is, not surprisingly, not commonly used but you should remember that this is what is really going on or you will get very confused when addresses, as distinct from contents of addressed locations, are the subject of arithmetic in more advanced programming.
All variables used in a program must be declared. All variable declarations must appear before any executable statements.
The question of the initial values stored in memory locations will be discussed later.