UNIX C Programming

Chapter 4: Character Strings and Formatted Input/Output



In this chapter students are introduced to characters strings and their use with printf() and scanf(). The objectives important to this chapter include:

  • Explain the meaning of a character string.
  • Provide several examples using printf()
  • Provide several examples using scanf()
  • Clarify the different types of conversion specifiers
  • Clarify the use of printf() and scanf() modifiers
  • Introduce the concept of a character array
  • Explain the strlen() function
  • Provide a number of #define preprocessor directives

A character string is a series of one or more characters. Strings are stored in an array of type char. A null character (\0) marks the end of a strings. In C, all strings end with this terminating character.

An array is an ordered sequence of data of the same data type. An example of a character array is:

	char name[40];

This statement reserves space to store 40 characters.

A string constant differs from a character constant. A string constant always contains a \0 terminating character.

The strlen() function is used to determine the length of a string in characters. When using strlen() remember to include the string.h header file.

Two types of constants are highlighted in this chapter. A symbolic constant uses a name to refer to a value. As an example:

	pi = 3.14;
	circumference = pi * diameter; 
/* where pi is the symbolic constant */

A manifest constant is used in conjunction with a #define preprocessor directive. As an example,

	#define PI 3.14 
	/* where pi is the manifest constant */

A manifest or defined constant is acted on during compile time. Whenever the program discovers a manifest constant in the program (such as PI) it substitutes the constant value specified (3.14 in the above example). Like doing a "search and replace" with a word processor.

Use manifest constants whenever a value such as PI must be used frequently in a program. If PI must be changed to include additional digits, only one statement in the entire program needs to be changed.

The printf() and scanf() functions are input/output functions. They are most versatile; however, were not part of the original definition of C.

The syntax of printf() features a control and a variable list section. The control section contains literal characters and conversion specifiers. Literal characters are actually printed. Conversion specifiers indicate the data type to be printed. The list of these specifiers is as follows:

%c 	Single character
%d 	Decimal integer
%ld 	Long decimal integer
%e 	Floating-point number, e-notation
%E 	Floating-point number, E-notation
%f 	Floating-point number, decimal notation
%g 	Use %f or %e whichever is shorter
%G 	Use %f or %E whichever is shorter
%i 	Signed decimal integer
%o 	Unsigned octal integer
%p 	A pointer
%s 	Character string
%u 	Unsigned decimal integer
%x 	Unsigned hexadecimal integer,
	 using hex digits Of
%X 	Unsigned hexadecimal integer,
	 using hex digits OF
%% 	Percent sign

Besides conversion specifiers, printf() features several modifiers. The five printf() flags are -, +, space, #, and 0. A field width can also be set, as in %4d. Other modifiers are a period (precision), h (for short), l (for long), and L (for long double).

Both printf() and scanf() return values. The number of characters printed is returned by printf(); the number of characters read is returned by scanf().

Like printf(), scanf() uses a control string followed by a list of arguments. Two rules must be remembered when working with scanf():

  1. If you use scanf() to read a value for one of the basic variable types, precede the variable name with an &.
  2. If you use scanf() to read a string into a character array, do not use an &.

The scanf() function uses a set of conversion specifiers similar to printf()'s set.

The scanf() modifiers include * (suppress alignment), digit(s) (used for setting field width), and h, l, and L.

If a field width is used, scanf() halts at the field end or at the first whitespace, whichever comes first.