Mask-group

Formatted Console I/O Functions



  • Formatted Console I/O functions allow us to supply the input in a fixed format and let us obtain the output in the specified form.

  • The format string can contain:

  1. Characters that are simply printed as they are

  2. Conversion specifications that begin with a % sign

  3. Escape sequences that begin with a \ sign 

  • Example:

  • The %d and %f used in the printf( ) are called format specifiers. They tell printf( ) to print the value of average as a decimal integer and the value of percentage as a float. 

Optional specifiers in the format specifiers:

Specifier

Description

dd

Digits specifying field width

.

Decimal point separating field width from precision (precision stands for the number of places after the decimal point)

dd

Digits specifying precision

-

minus sign for left justifying the output in the specified field width

Escape Sequence:

Esc. Seq.

Purpose

\n

New line

\b

Backspace

\f

From feed

\'

Single quote

\\

Backslash

\t

Tab

\r

Carriage return

\a

Alert

\"

Double quote

  • \b moves the cursor one position to the left of its current position. 

  • \r takes the cursor to the beginning of the line in which it is currently placed. 

  • \a alerts the user by sounding the speaker inside the computer.

Sprintf( ) Functions:

  • The sprintf( ) function is similar to the printf( ) function except instead of sending the output to the screen as printf( ) does, this function writes the output to an array of characters. 

  • The printf( ) prints out the values on the screen, whereas sprintf( ) stores the values in the character array.