Types

For the moment we will only use variables of the simple, predefined types, i.e.
int
meaning values of whole numbers; other versions of int include long, short, unsigned, unsigned short and unsigned long;

Although C does not define either a range or a size in memory for these variants, it is safe to assume that a variable declared to be long has at least as large a range and occupies at least as much memory as an int. Similarly a short occupies no more memory and has no larger a range than an int;

The use of unsigned variables is usually when an integral value is intended to represent a bit pattern. The unsigned variants are usually capable of representing a larger range of positive values than their signed equivalent,

float
meaning values with possible fractional parts, known in other languages as real; other forms are double and long double;

As with the variants of integral values, C does not force a particular set of ranges or representations to correspond to these variants; it is safe to assume that the variants double and long double will be no smaller in range or size than float;

char
meaning values representing characters; in C char is a sub-range of int; there is also unsigned char;

strictly speaking the C char type is simply the smallest possible variant of the integral value family; it is defined to represent the memory unit capable and normally used to hold a character value;

Boolean
meaning values of True or False. In C these values are stored in int variables, with True represented as any non-zero value and False as zero.
When it is declared, we state the type of value that a variable can hold. If you place another type of value there, C forces the program to try to cast or convert it to the type for which that location was intended. If such a conversion is not possible, the attempt is illegal and should be rejected by the compiler.

Examples of declarations are

int IntVal;          /*Declares one integer variable, IntVal*/

float RVal1, RVal2;  /*Declares 2 real variables, RVal1 & RVal2*/

char Operator;        /*Declares one char variable, Operator*/
Note that each declaration may define one or more variables of the same type.

Exercises on this section.


Next.

Back to Contents page.