Escape Sequences and Formatted I/O Functions

Escape sequences

The following escape sequences allow special characters to be put into the source code.

Escape
Sequence
Name Meaning
\a Alert Produces an audible or visible alert.
\b Backspace Moves the cursor back one position (non-destructive).
\f Form Feed Moves the cursor to the first position of the next page.
\n New Line Moves the cursor to the first position of the next line.
\r Carriage Return Moves the cursor to the first position of the current line.
\t Horizontal Tab Moves the cursor to the next horizontal tabular position.
\v Vertial Tab Moves the cursor to the next vertical tabular position.
\’ Produces a single quote.
\” Produces a double quote.
\? Produces a question mark.
\\ Produces a single backslash.
\0 Produces a null character.
\ddd Defines one character by the octal digits (base-8 number)
\xdd Defines one character by the hexadecimal digit.

Examples:

p rintf(“\12”); – Produces the decimal character 10 (x0A Hex).

printf(“\xFF”); – Produces the decimal character – 1 or 255

printf(“\x123”); – Produces a single character

 

Formatted I/O Functions

printf Functions

int printf(const char * format,…);

int sprint(char *str, const char * format, …);

The printf functions provide a means to output formatted information to a stream.

printf sends formatted output to stdout

sprintf sends formatted output to a string

These functions take the format string specified by the format argument and apply each following argument to the format specifiers in the string in a left to right fashion. Each character in the format string is copied to the stream except for conversion characters which specify a format specifier.

 

A conversion specifier begins with the % character. After the % character come the following in this order:

[flags]                   Control the conversion (optional).

[width]                 Defines the number of characters to print (optional).

[.precision]        Defines the amount of precision to print for a number type (optional).

[modifier]           Overrides the size (type) of the argument (optional).

[type]                   The type of conversion to be applied (required).

 

Width:

The width of the field is specified here with a decimal value. If the value is not large enough to fill the width, then the rest of the field is padded with spaces. If the value overflows the width of the field, then the field is expanded to fit the value.

 

Precision:

The precision begins with a dob (.) to distinguish itself from the width specifier. The precision can be given as a decimal value or as an asterisk (*). If a * is used, then the next argument (which is an int type) specifies the precision.

 

scanf Functions

Declarations:

int scanf(const char * format, …);

int sscanf(const char * str, const char * format, …);

The scanf functions provide a means to input formatted information from a stream.

scanf reads formatted input from stdin

sscanf reads formatted input from a string

These functions take input in a manner that is specified by the format argument and store each input field into the following arguments in a left to right fashion. Each input field is specified in the format string with a conversion specifier which specifies how the input is to be stored in appropriate variable.

Char I/O

getchar : getchar reads a single character from standard input.

Syntax :                int getchar();

It requires the user to press enter after entering character.

Putchar : putchar writes a single character to standard output.

Syntax:                 int putchar(int value)

 

Leave a Reply

Your email address will not be published. Required fields are marked *