Formatted I/O in C

The functions which fall in this category provide a convenient way to format the input and/or output data as per the requirements. The two main functions within this category are printf() and scanf().

printf()
printf() function writes to the standard output (monitor) a sequence of data, formatted as per the format specifier. The general syntax of printf() function is as follows:

Screenshot from 2020-07-11 20-46-49

where formatString contains a list of format specifiers indicating the format and type of data to be written to the screen. The data is stored in the corresponding argument. The argument can be numeric values, strings of characters, variables, expressions, and so on. formatString can contain any mix of the following
1. Literal text to be displayed on screen,
2. Escape sequences used as carriage control,
3. And, format specifiers for conversion of data in the arguments to a display format.

It should be noted that printf() function returns the number of characters displayed. Also, after the formatString parameter, depending on the formatString, the function may expect a sequence of additional arguments, each containing one value to be inserted instead of each format specifier in the formatString parameter.

The format specifier follows this prototype:

Screenshot from 2020-07-11 20-48-52

Where specifier is the most important which defines the type and the interpretation of the value of the corresponding argument. TableĀ  enumerates various specifiers supported by printf() function along with the interpretation and display format of the corresponding argument.

Screenshot from 2020-07-11 20-50-12

Screenshot from 2020-07-11 20-50-49

Table : List of various specifiers supported by printf() function.

The format Specifier can also contain flags, width, .precision and length fields which are optional and follow these specifications:

Screenshot from 2020-07-11 20-52-05

Screenshot from 2020-07-11 20-53-10

Screenshot from 2020-07-11 20-54-27

Screenshot from 2020-07-11 20-55-24

Table : Lists and discusses the meaning of various fields of format-
specifier

ProgramĀ  shows some of the format specifiers for printf() function in action followed by the screenshot of the output.

Screenshot from 2020-07-11 20-56-52

Screenshot from 2020-07-11 20-57-49

Progam : Program shows some of the format specifiers of printf()
function in action

Screenshot from 2020-07-11 20-58-57

scanf()
The scanf() function scans the data input through the keyboard and by default delimits values by whitespace. Whitespace is defined as being a TAB, a blank or the newline character (‘\n’). Therefore, data that is input with the intention of having embedded blanks as part of the data value will be broken into several values and distributed among the input variables specified in the scanf() statement. The general syntax of the function is as follows

Screenshot from 2020-07-11 21-00-24

where formatString is a list of format specifiers indicating the format and type of data to be read from the keyboard and stored in the corresponding address. There must be the same number of format specifiers and addresses. scanf() returns the number of input fields successfully scanned, converted, and stored. The return value does not include scanned fields that were not stored.

formatString contains one or more of the following items:

1. Whitespace character: The function will read and ignore any whitespace characters (this includes blank spaces and the newline and tab characters) which are encountered before the next non-whitespace character. This includes any quantity of whitespace characters, or none.

2. Non-whitespace character, except % sign: Any character that is not either a whitespace character (blank, newline or tab) or part of a format specifier (which begin with a % character) causes the function to read the next character from stdin, compare it to this non-whitespace character and if it matches, it is discarded and the function continues with the next character of format. If the character does not match, the function fails, returning and leaving subsequent characters of stdin unread.

3. Format specifiers: A sequence formed by an initial percentage sign (%) indicates a format specifier, which is used to specify the type and format of the data to be retrieved from stdin and stored in the locations pointed by the additional arguments. A format specifier follows this prototype:

%[*][width][modifiers]type

where

Screenshot from 2020-07-12 17-30-41

The type field can have following values:

Screenshot from 2020-07-12 17-32-41

Screenshot from 2020-07-12 17-33-29

The function expects a sequence of references as additional arguments, each one pointing to an object of the type specified by their corresponding format-specifier within the formatString, in the same order. For each format specifier in the formatString that retrieves data, an additional argument should be specified. These arguments are expected to be references (pointers); so to store data in a regular variable, it should be precede with the reference operator, i.e. an ampersand sign (&), like as follows:

int n;
scanf (“%d”,&n);

Program shows some of the format specifiers of scanf() function in action followed by the screenshot of the output.

#include <stdio.h>
void main ()
{
char str [80];
int i;
printf (“Enter your family name: “);
scanf (“%s”,str);
printf (“Enter your age: “);

scanf (“%d”,&i);
printf (“Mr. %s , %d years old.\n”,str,i);
printf (“Enter a hexadecimal number: “);
scanf (“%x”,&i);
printf (“You have entered %#x(%d).\n”,
i,i);
}

Program: Program shows some of the format specifiers of scanf() function in action.

Screenshot from 2020-07-12 17-38-12