Ch. 9 -- C Formatted Input/Output

I. Streams -- 3 streams are automatically opened with a program is loaded for execution:

A. stdin

1. the standard input stream

2. usually connected to the keyboard buffer

B. stdout

1. the standard output stream

2. usually connected to the screen

C. stderr

1. the standard error stream

2. usually connected to the screen

II. Formatting Output with printf

A. A general purpose i/o instruction -- works with various formats

B. format control string (or “conversion specification string”)

1. conversion specifiers

2. flags

3. field widths

4. precisions

5. literal characters

6. wild cards

III. Printing Integers -- integer conversion specifiers

A. %d, %i -- display a signed decimal integer, either signed or unsigned

B. %o -- display an unsigned integer in octal format (base 8)

C. %u -- display an unsigned integer in decimal format (base 10)

D. %x or %X -- display an unsigned integer in hexadecimal format (base 16)

E. %h or %l -- placed before an integer conversion specifier to indicate that a short

or long integer is displayed respectively (“length modifiers”)

IV. Printing Floating-Point Numbers -- conversion specifiers

A. %e or %E

1. display a floating-point number in exponential notation

2. default is 6 digits of precision

B. %f

1. display a floating-point number in fixed-point notation

2. default is 6 digits of precision

C. %g or %G

1. display a floating-point value in either fixed-point or exponential format,

depending on the magnitude of the value

2. trailing zeros are not displayed

3. at least one non-zero decimal digit is required for the decimal point to be

displayed

4. l -- place before any floating-point conversion specifier to indicate that a

long double floating-point value is to be displayed (use “%lf” on

omega for type “double”)

V. Printing Strings and Characters -- conversion specifiers

A. %c -- for an argument of type ‘char’

1. will print the character and leave the file pointer pointing to the next

location

B. %s -- for a string (the address of a null-terminated array of characters)

1. will print each character until a ‘\0’ is encountered

2. will not print the ‘\0’ character

VI. Other Conversion Specifiers

A. %p -- displays an address in an implementation-defined manner

B. %n --

1. stores the # of characters already output in the current printf statement

2. the argument is a pointer to an integer variable in which the value is

stored

3. nothing is printed by a %n

C. % without a specifier causes the ‘%’ character to be displayed

VII. Printing with Field Widths and Precision

A. Field Widths

1. %4d -- display an integer in a field with of 4, right justified

2. %-4d -- display an integer in a field width of 4, left justified

3. %2c -- display a single character in a field width of 2, right justified

4. %-2c -- display a single character in a field width of 2, left-justified

5. %9s -- display a string in a field width of 9, right justified

6. %-9s -- display a string in a field width of 9, left-justified

B. Precisions -- has different meanings for different types

1. Default precisions

a. Integers : 1

b. e, E, and f -- the # of digits to appear after the decimal point

c. g, G -- the maximum # of significant digits to be printed

2. %A.Bf or %A.Blf

a. A must be big enough to accommodate the number of characters to be

displayed including the decimal point and the precision

b. %5.2f --print in a field width of 5 with a precision of 2, right

justified

(1) will work for 3.29, 32.29, 322.90

(2) too small for 132.94, 13.294, 1.3294

c. %-5.2f -- print in a field width of 5 with a precision of 2, left-

justified

VIII. Using Flags in the printf Format Control String

A. +

1. %+d -- right justified

2. +%d -- + will be displayed before the value

B. -

1. %-d -- left justified

2. -%d -- - will be displayed before the value

C. ‘ ‘ -- print a space before a positive value not printed with the + flag

D. #

1. prefix a 0 (zero) to the output value when used with octal specifier

2. prefix 0x or 0X to hexadecimal output printed using %x or %X

3. Force a decimal point for a floating-point number printed with e, E, f, g,

or G that does not contain a fractional part.

E. 0 -- pad a field with leading zeros

IX. Printing Literals and Escape Sequences

A. \’ -- display a single quote

B. \” -- display a double quote

C. \? -- display a ?

D. \\ -- display a \

E. \a -- cause an audible bell or visual alert

F. \b -- move the cursor back one position on the current line

G. \f -- move the cursor to the start of the next logical page

H. \n -- move the cursor to the beginning of the next line

I. \r -- move the cursor to the beginning of the current line

J. \t -- move the cursor to the next horizontal tab position

è device dependent!

K. \v -- move the cursor to the next vertical tab position

X. Formatting Input with scanf

A. Integers

1. scanf(“%d”, address_of_optionally_signed_int_variable);

2. scanf(“%i”, address_of_an_optionally_signed_decimal, _octal, or

_hex_integer);

3. scanf(“%o”, address_of_an_unsigned_integer);

4. scanf(“%u”, address_of_an_unsigned_integer);

5. scanf(“%x”, address_of_an_unsigned_integer);

or

scanf(“%X”, address_of_an_unsigned_integer);

6. scanf(“%ld”, address_of_a_long_integer);

7. scanf(“%hf”, address_of_a_short_integer);

Using a field width with an input conversion specification specifies the number

of characters to be read from the input stream.

B. Floating-point numbers

1. e, E, f, g, or G -- use an argument which is the address of a floating-

point variable

2. l or L -- a prefix to indicate that a double or long double is to be

input

C. Characters and strings

1. scanf(“%c”, address_of_a_char);

2. scanf(“%s”, address_of_an_array_of_characters);

a. argument must be of sufficient length

b. a terminating null (‘\0’) character is added.

D. scan set -- a set of characters enclosed in square brackets and preceded by a %

sign in the format control string.

1. Will scan the characters in the input stream, looking only for those

characters that match characters in the scan set.

2. Each time a character is matched, it is stored in the scan set’s

corresponding argument, which is the address of a character array.

3. The scan set stops inputting characters when a character is encountered that

does not match a character in the scan set.

4. If the first character in the input stream does not match a character in the

scan set, only the null character is stored in the array.

D. Miscellaneous

1. p -- red an address of the same form produced when using %p in a printf

2. n -- store the number of characters input so far in this scanf, with an

argument which is a address of an integer

3. % -- skip a % sign in the input

E. Wild cards -- asterisks

1. #define FIELD_WIDTH 5

#define PRECISION 2

main()

{

int X = 6;

double Y = 7.345;

printf(“%*d”, FIELD_WIDTH, X); // prints 6 in a field width of 5, right

// justified

printf(“%*.*lf”, FIELD_WIDTH, PRECISION, Y);

}

XI. Formatting input with sscanf

A. used to divide an existing string into tokens

B. the last conversion specifier should be a “%*c”, indicating to read the null

csharacter at the end of the string, but to not process it (no matching argument

needed)

XII. Formatting input with sprintf

A. used to specify arguments to replace conversion specifiers in a conversion

specification string

B. used to store the resulting specification string in a character arrays

4