CSC 161 Grinnell College Spring, 2010 Imperative Problem Solving and Data Structures

# Characters and Strings in C

• King, Sections 13.1-13.6, pages 277-300.

## References

This lab assumes that you have access to a C programming language reference, particularly for the C string function library. If you do not have a published reference in paper form, you might consider the following on-line reference:

C provides two equivalent approaches for reading individual characters.

• The C function getchar() reads and returns a single character.
Example:
```
char ch;
ch = getchar();
```
• The c format for scanf reads an individual character.
Example:
```
char ch;
scanf ("%c", &ch);
```

When reading character data with either getchar or %c with scanf, the first character is read and recorded; that is, the process of reading a character does NOT skip over white space.

C also provides at least three approaches for reading strings of characters. Each function has its own special characteristics.

• The s format for scanf reads a sequence of non-white-space characters. As with all strings, a null character ('\0') is added at the end of the string.
Example:
```
char str[10];      /* allow room for 10 characters, including the null */
scanf ("%s", str); /* since str is an array, the variable represents a
```
• The C function gets() reads an entire line or until end of file. As with scanf, a null character is added at the end of the string.
Example:
```
char str[10];
gets (str);
```
• The C function fgets() reads up to n characters from a line or until end of file, . As with scanf, a null character is added at the end of the string.
Example:
```
char str[10];             /* stdin is the C variable for "standard input" */
fgets (str, 10, stdin);   /* up to 9 characters are read from the
terminal, leaving room for a null character
at the end */

```

As with reading character data , stored input starts immediately with the first character read; the process of reading a string does NOT skip over white space.

Warning: Both scanf and gets read characters (until white space or the end of a line), without regard for the size of the string array. If more characters are read than fit in the array, the characters may overflow to fill data stored in other variables. Thus, only fgets can be considered "safe".

### Characters as Integers

The following example shows that char is considered to be a type of integer, and integer arithmetic may be performed on their values.

```    #include <stdio.h>

int main() {
char ch;
for (ch = 'A'; ch < 127; ch++) {
printf("Character: %c", ch);
printf("\t ASCII: %d \n", ch);
}

return 0;
}
```

The following example illustrates that some characters represent actions rather than just printing a symbol.

```    #include <stdio.h>

int main() {
char ringBell = '\a';
char tab = '\t';
char backspace = '\b';
char ch;

printf("Now hit the Enter key. \n");
ch = getchar();   /* wait for the user to enter something */
printf("Beep %c \n", ringBell);

printf("Now hit the Enter key. \n");
ch = getchar();   /* wait for the user to enter something */
printf("These %c words %c have %c tabs %c between %c them.\n",
tab, tab, tab, tab, tab);

printf("Now hit the Enter key. \n");
ch = getchar();   /* wait for the user to enter something */
printf("Can you read the word: hockey? %c%c%c%c%c%c%c%c         \n",
backspace, backspace, backspace, backspace,
backspace, backspace, backspace, backspace );

return 0;
}
```

This document is available on the World Wide Web as

```http://www.cs.grinnell.edu/~walker/courses/161.sp10/readings/reading-strings-c.shtml
```