Variable Names
Informally, a variable
(also called an object) is a place you can store a value. So that you can refer to it unambiguously, a variable needs a name. You can think of the variables in your program as a set of boxes or cubbyholes, each with a label giving its name; you might imagine that storing a value ``in’’ a variable consists of writing the value on a slip of paper and placing it in the cubbyhole.
A declaration
tells the compiler the name and type of a variable you’ll be using in your program. In its simplest form, a declaration consists of the type, the name of the variable, and a terminating semicolon:
char c;
int i;
float f;
You can also declare several variables of the same type in one declaration, separating them with commas:
int i1, i2;
Later we’ll see that declarations may also contain initializers, qualifiers and storage classes, and that we can declare arrays
, functions
, pointers
, and other kinds of data structures.
The placement of declarations is significant. You can’t place them just anywhere (i.e. they cannot be interspersed with the other statements in your program). They must either be placed at the beginning of a function, or at the beginning of a brace-enclosed block of statements (which we’ll learn about in the next chapter), or outside of any function. Furthermore, the placement of a declaration, as well as its storage class, controls several things about its visibility
and lifetime
, as we’ll see later.
You may wonder why variables must be declared before use. There are two reasons:
- It makes things somewhat easier on the compiler; it knows right away what kind of storage to allocate and what code to emit to store and manipulate each variable; it doesn’t have to try to intuit the programmer’s intentions.
- It forces a bit of useful discipline on the programmer: you cannot introduce variables willy-nilly; you must think about them enough to pick appropriate types for them. (The compiler’s error messages to you, telling you that you apparently forgot to declare a variable, are as often helpful as they are a nuisance: they’re helpful when they tell you that you misspelled a variable, or forgot to think about exactly how you were going to use it.)
Although there are a few places where declarations can be omitted (in which case the compiler will assume an implicit declaration), making use of these removes the advantages of reason 2 above, so I recommend always declaring everything explicitly.
Most of the time, I recommend writing one declaration per line. For the most part, the compiler doesn’t care what order declarations are in. You can order the declarations alphabetically, or in the order that they’re used, or to put related declarations next to each other. Collecting all variables of the same type together on one line essentially orders declarations by type, which isn’t a very useful order (it’s only slightly more useful than random order).
A declaration for a variable can also contain an initial value. This initializer consists of an equals sign and an expression, which is usually a single constant:
int i = 1;
int i1 = 10, i2 = 20;
Within limits, you can give your variables and functions any names you want. These names (the formal term is ‘‘identifiers’’) consist of letters, numbers, and underscores. For our purposes, names must begin with a letter. Theoretically, names can be as long as you want, but extremely long ones get tedious to type after a while, and the compiler is not required to keep track of extremely long ones perfectly. What this means is that if you were to name a variable, say supercalafragalisticespialidocious
, the compiler might get lazy and pretend that you’d named it supercalafragalisticespialidocio
, such that if you later misspelled it supercalafragalisticespialidociouz
, the compiler wouldn’t catch your mistake. Nor would the compiler necessarily be able to tell the difference if for some perverse reason you deliberately declared a second variable named supercalafragalisticespialidociouz
.
The capitalization of names in C is significant: the variable names variable
, Variable
, and VARIABLE
(as well as silly combinations like variAble
) are all distinct.
A final restriction on names is that you may not use keywords (the words such as int
and for
which are part of the syntax of the language) as the names of variables or functions (or as identifiers of any kind).