4.1 A New Data Type: char (2024)

4.1 A New Data Type: char (5)
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, 4.1 A New Data Type: char (6),is used as an escape character which signifies something specialor different from the ordinary and is followed by one characterto indicate the particular control character.We have already seen one such control sequence in our printf() statments; the newline character, '4.1 A New Data Type: char (7)n'.Other frequently used control character constants written withan escape sequence, include '4.1 A New Data Type: char (8)t' for tab, '4.1 A New Data Type: char (9)a' for bell, etc.Table 4.1 shows the escape sequences used in C.The newline, tab, and space characters are called white spacecharacters, for obvious reasons.4.1 A New Data Type: char (10)

4.1 A New Data Type: char (11)4.1 A New Data Type: char (12)

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 character
The code for this program is shown in Figure 4.1.

4.1 A New Data Type: char (13)

4.1 A New Data Type: char (14)4.1 A New Data Type: char (15)

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
The sample session shows that as entire lines of characters are entered;they are printed. Each character typed is not immediately printed,since no input is received by the program until a newline character is typed bythe user; i.e. the same buffering we saw for numeric data entry.When a newline is typed, the entire sequence ofcharacters, including the newline, is placed in the keyboard buffer and scanf() then reads input from the buffer, one character at a time,up to and including the newline.In our loop,each character read is then printed.When the buffer is exhausted, the next line is placed in the buffer and read,and so on.So, scanf() is behaving just as it did for numeric data; each callreads one data item, in this case a character ( %c).One notable difference between reading numeric data and character data isthatwhen scanf() reads a character, leading white space characters are read,one character at a time,not skipped over as it is when reading numeric data.
  • 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
4.1 A New Data Type: char (20)
Previous: 4 Processing Character Data
Up: 4 Processing Character Data
Next: 4.2 Sample Character Processing Functions
Previous Page: 4 Processing Character Data
4.1 A New Data Type:  char (2024)
Top Articles
Latest Posts
Article information

Author: Delena Feil

Last Updated:

Views: 5758

Rating: 4.4 / 5 (65 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Delena Feil

Birthday: 1998-08-29

Address: 747 Lubowitz Run, Sidmouth, HI 90646-5543

Phone: +99513241752844

Job: Design Supervisor

Hobby: Digital arts, Lacemaking, Air sports, Running, Scouting, Shooting, Puzzles

Introduction: My name is Delena Feil, I am a clean, splendid, calm, fancy, jolly, bright, faithful person who loves writing and wants to share my knowledge and understanding with you.