
A variable is a name for a place that holds a number. In C you say what kind of number before you use the name, at the top of the function, and then you may put values in it and take them out as often as you like. This section is that, plus the rules this compiler has about where a declaration may go, and one thing it lets you do that many Cs of its day did not: give a type a name of your own.
int apples, pears, fruit;
char initial;
Each declaration is a type, one or more names, and a semicolon. int and char and unsigned are the types from S9; the names are yours. Every variable must be declared before it is used, and every declaration must come at the start of the function, before the first statement - this compiler will not take one in the middle, nor inside an inner pair of braces (it says expecting a primary here). Declarations first, then the doing.
A name is letters, digits and _, starting with a letter, and case matters: Apples and apples are different names, and every keyword - int, char, if, while - must be in lower case. Names may be as long as you like; total_apples and total_pears are different, as they should be.
apples = 12;
fruit = apples + pears;
initial = 'G';
= puts the value on the right into the name on the left. It is not a statement of equality; it is an instruction, and apples = apples + 1 is a perfectly good one - take what is in apples, add one, put it back.
That instruction is so common that C has short forms for it. apples += 1 means the same, and so does apples++. There is -= and --, *=, /= and the rest.
A variable you have not given a value holds whatever was in that place before. In one run of the program below it was 2140. Give every variable a value before you read it.
You may want to give a variable its first value where you declare it. This compiler allows that in two cases:
int best = 30; /* outside any function */
main()
{
static int count = 10;
A variable declared outside any function - a global, known to every function in the file - may be given its value in the declaration. So may one inside a function that is marked static, which makes it a variable that lives for the whole program rather than for one call of the function. An ordinary local may not: int count = 10; inside a function is refused with RESTRICTION: use assignment or blt() to initialise automatics, which is the compiler's way of saying "declare it, then assign it on the next line". (blt is a library function for copying memory, for when the variable is an array; the course does not need it.)
typedef int score;
score best = 30;
typedef gives a type a new name. After the first line, score means int wherever a type is wanted, and score best is an int called best. It changes nothing about how the program runs; it says what a number is for, which is worth having when a program is full of ints that mean different things. It also has a job this compiler gives it that other Cs do not: S16 needs a typedef before it can turn a number into an address. The course will use it from there on.
#include STDIO.H
main()
{
int apples, pears, fruit;
char initial;
apples = 12;
pears = 30;
fruit = apples + pears;
initial = 'G';
printf("%d apples, %d pears, %d fruit\n", apples, pears, fruit);
printf("initial %c is code %d\n", initial, initial);
apples = apples + 1;
apples += 1;
apples++;
printf("now %d apples\n", apples);
}
#include ?STDIO.LIB?
Starting from
Typed as NAMES.C; compiled and run.
What you should see
12 apples, 30 pears, 42 fruit
initial G is code 71
now 15 apples
Three declarations you may reach for from habit, and what happens:
unsigned char c; - refused, bad declarator. A char is already unsigned here (S9); unsigned on its own is a type.void f() - works, but only because STDIO.H defines the word: it is a #define for int. A function that returns nothing may be written with no type at all, as main is, and S13 says which to use.register int i; - accepted, and, the manual says, ignored: the compiler has no registers to spare.int apples, pears, fruit; to after the first printf line. What does the compiler say, and at which line?int apples = 12; in place of the declaration and the assignment. What does the compiler say?typedef int count; at the top and declare apples as a count. Does anything change?10.1 Declare an int called year, set it to the current year, and print the year you were born from it and your age.
10.2 Swap two variables' values using a third, and print them before and after.
10.3 Declare a global int with its value in the declaration, print it from main, change it in main, and print it again.
Worked solutions are in Appendix II.
| Symptom | Cause |
|---|---|
undefined variable | The name is misspelt, or declared after its first use. |
expecting a primary here at a declaration | It is in the middle of the function, or inside inner braces. Move it to the top. |
RESTRICTION: use assignment or blt() to initialise automatics | int x = 1; inside a function. Declare, then assign; or static. |
bad declarator at unsigned char | There is no such type here; char is already unsigned. |
| A variable holds a value you never gave it | You never gave it one. |
Declare every variable at the top of the function, type name;. = assigns; += and ++ are short forms. Only globals and static variables may be given a value where they are declared. typedef names a type. Keywords are lower case, and names may be long.
S11, Sums And Choices - the operators, comparing values, if and else, and the difference between = and == that no compiler will catch for you.