C for Yourself - part 3

This month, David Matthewman starts to look at the different types of variable available in C.

In Fortran, whether a variable was an integer or not depended, by default, on its first letter. If it started with a letter between I and O inclusive, it was an integer, otherwise it was real. In BBC Basic, integer variables ended with a % sign, strings with a $.

In C, a variable can have any type, so the compiler needs to be told what type the variable is before it is used.

This is called declaring the variable. It usually happens on the first few lines in a program or procedure. Declarations, as mentioned last issue, have the format:

var_type var_list;

where var_list is a list of one or more variables of type var_type, separated by commas.

Variable types

The most common data types in C are:

The number of bytes taken up by these variables, and hence the maximum size of number that can be stored on them, can vary from compiler to compiler, although char is nearly always a single byte.

The int type is usually four bytes long on the Archimedes. It can be modified by the short and long declarations, as in:

short int pintsize;

or, equivalently:

short pintsize;

but on the Archimedes compilers, these all tend to produce four-byte numbers.

All the integer and char types can be signed or unsigned, though there is no need to make them either explicitly.

Integers are signed by default, but whether or not char variables are depends upon the compiler - they are unsigned in EasyC.

The program Signed on the disc will determine whether or not char variables are signed on a particular compiler.

Signed integers can hold the same range of values as unsigned ones but, whereas unsigned integers hold values from 0 to n, signed integers hold values from (roughly) -n/2 to n/2.

For instance, an unsigned character variable will hold values from 0 to 255, while a signed one will hold values from -128 to 127. Both types have a range of 256.

floating-point numbers of type double will usually - but not always - hold larger numbers to a high precision than those of type float.

There is a further type, long double, which may give more precision yet.

With the EasyC compiler, the largest float value is 3.40282374E38, and the largest double and long double values are 1.79769313486231571E308. Note the extra digits of precision in the double variable.

Having declared your variable, you will wish to assign a value to it. This can be done easily by a statement of the form:

i = 42;

though numbers may be specified in a number of different formats. They may be octal or hexadecimal: an octal number is prefixed with 0 (zero), and a hexadecimal one with 0x or 0X. The numbers 30, 036, 0x1e and 0X1E are equivalent. A number with an l or L suffix is defined to be long, and one with a, u or U suffix is unsigned.

Floating point variables can end in an exponent (6e23), contain a decimal point, or both.

Note that by default their type is double, although they can be suffixed with f or F to give them type float and with l or L to give them type long.

Characters are enclosed within single quotes. Non-standard characters can be represented by escape sequences starting with a backslash, \. For instance, a newline is:

'\n'

Variables of type char actually hold an integer representing the character in question, so it is perfectly legal C to write:

char char1, char2;
char1 = 48;
char2 = char1+8;

Though this will normally pose no problems, it is worth remembering that the actual integers assigned to the characters may change from machine to machine and from compiler to compiler.

It's not even safe to assume that if a given character is 'a', the character five up from it will be 'f'.

The program Characters on the cover disc demonstrates the escape sequences permitted for a character variable.

Initialisation

When variables are declared, they can also be initialised with a starting value. This is done by following the variable with an equals sign and a number or character, as in:

char initial = 'D';
int babylon = 5;
float small = 1.0e-5f;

If a declaration is preceded by the const keyword, it specifies that the variable's value will not be changed. For instance:

const double e = 2.71828182845905;

Exactly what happens if a program attempts to change the value of a constant variable depends on the compiler, but most compilers will get upset.

If you have a variable whose value you know to bo constant, it is therefore worth declaring it as a const, so that you will be warned if you try to change it.

Casting

What happens if you wish to assign, say, an integer value to a floating point variable? For instance, in the following code:

int i;
float temperature;
...
i = temperature;

This varies slightly from compiler to compiler, but the general behaviour is to compile the code correctly, but to warn the programmer that a cast from one type to another has taken place.

When we come to look at pointers, we will discover that the compiler will generate an error if an attempt is made to assign one type of pointer to another. C compilers like to be reassured when this type of conversion takes place.

The way to do this explicitly is to cast the variable into the desired type, by including the type in brackets before the variable.

In our example, we would write:

i = (int) temperature;

which tells the computer that you, the programmer, are aware that temperature is a floating point number, but that you wish it to be treated as an integer in this instance.

Next month I will look at performing operations on variables: adding, subtracting, bit-shifting and other factors.

Until then, you can look at the program HofLords on the cover disc, which declares a number of variables and assigns variables to them.

The values all match the declarations of the variables, so the program should give you some idea of how to write different types of number.


Source: Acorn User - 147 - October 1994
Publication: Acorn User
Contributor: David Matthewman