1. What are the formatted and unformatted functions?
Ans: The formatted input and output functions supported by C are scanf() and printf()
respectively. The scanf() reads the data into a variable from the keyboard and printf() prints/
writes the formatted data on the monitor screen. The letter f in the printf and scanf stands for
formatted. As per requirement, input and output data provided can be arranged in meticulous formatted.
It is essential for a programmer to provide proper conversion symbol/control string in the input and out-
put functions to identify the data type. The control string in the input and output functions create formatted
results. The return values are equal to the number of variables/arguments specified in the function and
it must match with the fields of format specified in the control string of input/output functions.
The syntax of formatted input function scanf() is as follows:
scanf(control_string, argument1, argument2,---------, argument n);
Example: scanf("%d",&x);
Where %d is the control string that specifies the field format in which the data is to be entered. The
other part is the variable x and a sign & (ampersand) must precede to x. The &x is the address location
of the variable x.
Similarly, the syntax of formatted output function printf() is as follows:
printf(control_string, argument1, argument2,---------, argument n);
Example: printf("%d %f %c",x,y,z);
Here the control string is %d %f %c (type of values) and arguments are x y z are the identifiers
whose data is to be printed.
Unformatted functions: The unformatted input/output functions work only with character data type.
They do not require format conversion symbol for the formatting of data types, because they work only
with character data type. There is no need to convert data.
In case values of other data types are passed to these functions, they are treated as character data.
A few examples of unformatted functions supported by C are as follows:
getche(), getch(), gets(), getchar(), puts() etc.
getch() accepts a character from a keyboard and can be stored in a variable.
Input and Output in C
4
M04_ITL-ESL4791_02_SE_C04.indd 68 12/22/2012 5:00:39 PM