## Section2.13Storing Characters

Almost all programs perform a great deal of text string manipulation. Text strings are made up of arrays of characters. The first program you wrote was probably a “Hello world” program. If you wrote it in C, you used a statement like:

printf("Hello world\n");


or in C++:

cout << "Hello world" << endl;


When translating either of these statements into machine code, the compiler must do two things:

1. store each of the characters in a location in memory where the control unit can access them, and

2. generate the machine instructions to write the characters on the screen.

We start by considering how a single character is stored in memory. There are many codes for representing characters, but the most common one is the American Standard Code for Information Interchange (ASCII—pronounced “ask' e”). It uses seven bits to represent each character. Table 2.13.1 shows the bit patterns for each character in hexadecimal. If you are at your computer, you can generate this table by typing the command man ascii.

This is not the sort of table that you would memorize. However, you should become familiar with some of its general characteristics. In particular, notice that the numerical characters, 09, are in a contiguous sequence in the code, $\hex{30}$–$\hex{39}\text{.}$ The same is true of the lower case alphabetic characters, az, and of the upper case characters, AZ. Notice that the lower case alphabetic characters are numerically higher than the upper case.

The codes in the left-hand column of Table 2.13.1, $\hex{00}$–$\hex{1f}\text{,}$ define control characters. The ASCII code was developed in the 1960s for transmitting data from a sender to a receiver. If you read some of names of the control characters, you can imagine how they could be used to control the “dialog” between the sender and receiver. They can be generated on a keyboard by holding the control key down while pressing an alphabetic key. For example, ctrl-d generates an EOT (End of Transmission) character.

ASCII codes are usually stored in the rightmost seven bits of an eight-bit byte. The eighth bit (the highest-order bit) is called the parity bit. It can be used for error detection in the following way. The sender and receiver would agree ahead of time whether to use even parity or odd parity. Even parity means that an even number of ones is always transmitted in each characters; odd parity means that an odd number of ones is transmitted. Before transmitting a character in the ASCII code, the sender would adjust the eighth bit such that the total number of ones matched the even or odd agreement. When the code was received, the receiver would count the ones in each eight-bit byte. If the sum did not match the agreement, the receiver knew that one of the bits in the byte had been received incorrectly. Of course, if two bits had been incorrectly received, the error would pass undetected, but the chances of this double error are remarkably small. Modern communication systems are much more reliable, and parity is seldom used when sending individual bytes.

In some environments the high-order bit is used to provide a code for special characters. A little thought will show you that even all eight bits will not support all languages, e.g., Greek, Russian, Chinese. The Unicode character standard was first introduced in 1987 and has evolved over the years. It includes additional bytes so it can handle other alphabets. Unicode is backwards compatible with ASCII. We will only use ASCII in this book.

A computer system that uses an ASCII video system can be programmed to send a byte to the screen. The video system interprets the bit pattern as an ASCII code (from Table 2.13.1) and displays the corresponding character on the screen.

Getting back to the text string, "Hello world\n", the compiler would store this as a constant array of characters. There needs to be a way to specify the length of this array. In a C-style string this is accomplished by using the sentinel character NUL at the end of the string. So the compiler must allocate thirteen bytes for this string. An example of how this string is stored in memory is shown in Figure 2.13.2. Notice that C uses the LF character as a single newline character even though the C syntax requires that the programmer write two characters, “\n”. The area of memory shown includes the three bytes immediately following the text string.

In Pascal the length of the string is specified by the first byte in the string. It is taken to be an 8-bit unsigned integer. So C-style strings are typically processed by sentinel-controlled loops, and count-controlled string processing loops are more common in Pascal. The C++ string class has additional features, but the actual text string is stored as a C-style text string within the C++ string instance.