C Programming

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[18];
	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

	Hello<enterkey>
then
	string[0] = 'H'
	string[1] = 'e'
	....
	string[4] = 'o'
	string[5] = '\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.