Previous: 4 Processing Character Data
Up: 4 Processing Character Data
Next: 4.2 Sample Character Processing Functions
Previous Page: 4 Processing Character Data
Thecomplete set of characters that can be recognized by the computer is called the character set of the machine.As with numbers, the representation in the computer of each characterin the set isdone by assigning a unique bit pattern to each character.The typical character set consists of the following types of characters:
Alphabetic lower case: 'a',..., 'z' Alphabetic upper case: 'A',..., 'Z' Digit symbols : '0',..., '9' Punctuation : '.', ',', ';', etc. Space : ' ' Special symbols : '@', '#', '$', etc. Control Characters : newline, tab, bell or beep, etc.For example,a digit symbol is character type data, so when we type 234 at the keyboard, weare typing a sequence of character symbols: '2', followed by '3',followed by '4'.The function scanf() takes this sequenceand converts it to the internal form ofthe equivalent number, 234.Similarly, all writing on the screen is asequence of characters so printf() takes the internal form of the number andconverts it to a sequence of characters which are written onto the screen.
In C programs,variables may be declared to hold a single character data item byusing the keyword char as the type specifier in the declarationstatment:
char ch;A character constant is written surrounded bysingle quotation marks, e.g. 'a', 'A', '$', '!', etc.Only printable character constants can be written insingle quotes, not control characters, sowriting of non-printable control character constants requiresspecial handling. In C, the backslash character,
Let us consider a simple task of reading characters typed at the keyboard andwriting them to the screen. The task is to copy (or echo) the charactersfrom the input to the output.We will continue this task until there is no more input,i.e. until the end of the input file.
TASK
COPY0: Write out each character as it is read until the end of input file.
The algorithm can be stated simply as:
read the first character while there are more characters to read write or print the previously read character; read the next characterThe code for this program is shown in Figure 4.1.
The keyword char declares a variable, ch,of character data type. We also declare aninteger variable, flag, to save the value returned by scanf().Recall, the value returned iseither the number of items read by scanf() or the value EOFdefined in stdio.h.(We do not need to know the actual value of EOF to use it).
After the title is printed, a character is read by the statement:
flag = scanf("%c", &ch);The conversion specification for character type data is %c, sothis scanf() reads a single character from the input.If it is not an end of file keystroke, the characterread is stored into ch, and the value returned by scanf(), 1,is saved in flag. As long as the value of flag is not EOF,the loop is entered.The loop body first prints the value of ch, i.e. the lastcharacter read, and then, the assignment statement reads a new character andupdates flag.The loop terminates when flag is EOF, i.e. when an end of filekeystroke is detected.Remember, scanf()does not store the value, EOF into the object, ch.DO NOT TEST THE VALUE OF ch FOR EOF,TEST flag.A sample session is shown below:
- ***Copy Program***
- Type text, terminate with EOF
- Now is the time for all good men
- Now is the time for all good men
- To come to the aid of their country.
- To come to the aid of their country.
- '136D
- 4.1.1 The ASCII Character Set
- 4.1.2 Operations on Characters
- 4.1.3 Character I/O Using getchar() and putchar()
- 4.1.4 Strings vs Characters
Previous: 4 Processing Character Data
Up: 4 Processing Character Data
Next: 4.2 Sample Character Processing Functions
Previous Page: 4 Processing Character Data