Выбрать главу

Enumerated constants are integer type and are used to make a program easier to follow. In the following example, constant colors stores the names of colors. The first element is given the value 0:

enum colors {black, brown, red, orange, yellow, green, blue, gray, white};

3.1.9 Escape Sequences

Escape sequences are used to represent nonprintable ASCII characters. Table 3.3 shows some commonly used escape sequences and their representation in C language. For example, the character combination “\n” represents the newline character.

Table 3.3: Some commonly used escape sequences

Escape sequence Hex value Character
\a 0x07 BEL (bell)
\b 0x08 BS (backspace)
\t 0x09 HT (horizontal tab)
\n 0x0A LF (linefeed)
\v 0x0B VT (vertical feed)
\f 0x0C FF (formfeed)
\r 0x0D CR (carriage return)
\xH   String of hex digits

An ASCII character can also be represented by specifying its hexadecimal code after a backslash. For example, the newline character can also be represented as “\x0A”.

3.1.10 Static Variables

Static variables are local variables used in functions (see Chapter 4) when the last value of a variable between successive calls to the function must be preserved. As the following example shows, static variables are declared using the keyword static:

static unsigned int count;

3.1.11 External Variables

Using the keyword extern before a variable name declares that variable as external. It tells the compiler that the variable is declared elsewhere in a separate source code module. In the following example, variables sum1 and sum2 are declared as external unsigned integers:

extern int sum1, sum2;

3.1.12 Volatile Variables

Volatile variables are especially important in interrupt-based programs and input-output routines. Using the keyword volatile indicates that the value of the variable may change during the lifetime of the program independent of the normal flow of the program. Variables declared as volatile are not optimized by the compiler, since their values can change unexpectedly. In the following example, variable Led is declared as a volatile unsigned char:

volatile unsigned char Led;

3.1.13 Enumerated Variables

Enumerated variables are used to make a program more readable. In an enumerated variable, a list of items is specified and the value of the first item is set to 0, the next item is set to 1, and so on. In the following example, type Week is declared as an enumerated list and MON = 0, TUE = 1, WED = 2, and so on):

enum Week {MON, TUE, WED, THU, FRI, SAT, SUN};

It is possible to imply the values of the elements in an enumerated list. In the following example, black = 2, blue = 3, red = 4, and so on.

enum colors {black = 2, blue, red, white, gray};

Similarly, in the following example, black = 2, blue = 3, red = 8, and gray = 9:

enum colors {black = 2, blue, red = 8, gray};

Variables of type enumeration can be declared by specifying them after the list of items. For example, to declare variable My_Week of enumerated type Week, use the following statement:

enum Week {MON, TUE, WED, THU, FRI, SAT, SUN} My_Week;

Now we can use variable My_Week in a program:

My_Week = WED // assign 2 to My_Week

or

My_Week = 2 // same as above

After defining the enumerated type Week, we can declare variables This_Week and Next_Week of type Week as:

enum Week This_Week, Next_Week;

3.1.14 Arrays

Arrays are used to store related items in the same block of memory and under a specified name. An array is declared by specifying its type, name, and the number of elements it will store. For example:

unsigned int Total[5];

This array of type unsigned int has the name Total and has five elements. The first element of an array is indexed with 0. Thus, in this example, Total[0] refers to the first element of the array and Total[4] refers to the last element. The array Total is stored in memory in five consecutive locations as follows:

Total[0]
Total[1]
Total[2]
Total[3]
Total[4]

Data can be stored in the array by specifying the array name and index. For example, to store 25 in the second element of the array we have to write:

Total[1] = 25;

Similarly, the contents of an array can be read by specifying the array name and its index. For example, to copy the third array element to a variable called Temp we have to write:

Temp = Total[2];

The contents of an array can be initialized during the declaration of the array by assigning a sequence of comma-delimited values to the array. An example follows where array months has twelve elements and months[0] = 31, months[1] = 28, and so on:

unsigned char months[12] = {31,28,31,30,31,30,31,31,30,31,30,31};

The same array can also be declared without specifying its size:

unsigned char months[] = {31,28,31,30,31,30,31,31,30,31,30,31};

Character arrays can be declared similarly. In the following example, a character array named Hex_Letters is declared with 6 elements:

unsigned char Hex_Letters[] = {'A', 'B', 'C', 'D', 'E', 'F'};

Strings are character arrays with a null terminator. Strings can be declared either by enclosing the string in double quotes, or by specifying each character of the array within single quotes and then terminating the string with a null character. The two string declarations in the following example are identical, and both occupy five locations in memory: