READING CHARACTER STRINGS FROM THE KEYBOARD
Character based arrays are often refered to in C as strings. C does not support a string type, so character based arrays are used in place of strings. The %s modifier to printf() and scanf() is used to handle character based arrays. This assumes that a 0 or NULL value is stored in the last element of the array. Consider the following, which reads a string of characters (excluding spaces) from the keyboard.
char string; scanf("%s", string);
NOTE that the & character does not need to precede the variable name when the formatter %s is used! If the users response was
string = 'H' string = 'e' .... string = 'o' string = '\0'
Note how the enterkey is not taken by scanf() and the text string is terminated by a NULL character '\0' after the last character stored in the array.
ęCopyright B Brown. 1984-1999. All rights reserved.