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() 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:
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:
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.
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:
Table : Lists and discusses the meaning of various fields of format-
Program shows some of the format specifiers for printf() function in action followed by the screenshot of the output.
Progam : Program shows some of the format specifiers of printf()
function in action
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
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:
The type field can have following values:
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:
Program shows some of the format specifiers of scanf() function in action followed by the screenshot of the output.
void main ()
char str ;
printf (“Enter your family name: “);
printf (“Enter your age: “);
printf (“Mr. %s , %d years old.\n”,str,i);
printf (“Enter a hexadecimal number: “);
printf (“You have entered %#x(%d).\n”,
Program: Program shows some of the format specifiers of scanf() function in action.