Write about Formatted input/output Functions in C

https://www.computersprofessor.com/2016/12/write-about-formatted-inputoutput.html
Reading data from input
device and displaying the result on the screen are the two main tasks of any
programming. In ‘C’ language the input output operations are carried out by
using standard functions. The programs for these functions are kept in the
file
#include
1. formatted functions
2. Unformatted
functions.
Formatted
functions:–
The formatted I/O functions read
& write all types of data values. They require conversion symbol to
identify the data type.
scanf(_):–
This is formatted i/p function which
can read data from the standard i/p device into the variable in different
formats by the user.
The function is used to read data
more than one variable at a time.
Syntax:– scanf(“control string”,
&arg);
Ex:–scanf(“%d”,&no);
1. it reads all types of data
values.
2. It is used for run time
assignment of variables.
3. It requires conversion symbol to
identify the data to be read by the program during execution.
4. The ampersand(&) symbol is
used to indicate the memory location of the variable. So that the value read
would be placed at that location.
5. The control string may contain
format code, escape sequence & width specifier.
6. The format code specifies the
type of input data.
7. For each variable there must be a
format code.
Syntax:scanf(“control
stirng”&arg1, &arg2, . . . ,&argn);
The control string specifies the
field format in which the data is to be
entered & the arg.s arg1,…. arg n specify the addition of the
locations where the data is stored.
Control string & arguments are
separated by commas.
Control string contains filed
specifications, which direct the interpretation of i/p data. If may include:
1. Field (or format) specifications,
consisting of the conversion character %, a data type character and an
optional nunmer specifying the field width.
2. Blanks, tabs, or new lines.
The data character indicates the
type of data that is to be assigned to the variable associated with the
corresponding argument.
The field width specifies is optional.
Commonly used scanf. format codes
are:
Inputting
integer numbers:–
The field specification for reading
a integer is
:
Syntax:– %wd
The % sign indicates that a
conversion specification follows. W is a integer number that specifies the
field width of the number to be read and d, known as data type character,
indicates that the number to be read is in integer mode.
Ex:– scanf(“%2d%5d”,
&no1, &no2);
An i/p field may be skipped by
specifying in the place of field width.
Ex:– scanf(“%d%*d%d”, &a, &b);
Will assign the data 123 456 789
Now 123 is assigned to a 456 is
skipped (because of *) 789 is to b
Inputting real
numbers:–
The field width of real numbers is
not to be specified and therefore scanf reads real numbers using the simple
specification %f for both the notification.
Ex:– scanf(“%f%f%f”, &x,
&y, &z);
With the i/p data 475.1 43.21–1 678
Will assign the values:475.1 to x
4.321 to y and
678.0 to z
Inputting
character strings:
It is done in 2 ways, by using
getchar function and by using scanf function.
A scanf function can input strings
containing more than one character.
Syntax: %ws or %wc
The corresponding argument should be
a pointer to a character array.
%c is used to read a single
character.
%s is used to read words or
paragraphs.
Reading mixed
data types:–
It is possible to use one scanf
statement to input a data line containing mixed mode data. in such cases,
care should be exercised to ensure that the i/p data items match the control specifications in
order and type.
Ex:– scanf(“%d%c%f%s”,
&no, &code, & ration, name);
Detection of errors in
input:
Rules while using scanf:–
1.Every variable to be read must have field specification.
2. For each field specification
there must be variable address Of proper
type.
3. Never end the format string with
white space. It is a fatel error.
4. Scanf read until.
·
A
white space character is formed.
·
The
maximum no of characters have been read.
·
An
error is detected.
·
The
end of file is reached
Formatted out
put:
Printf():–
Pritnf function is used to print the
data (or) more than one data item in different formats on the standard o/p
device.
Syntax:–printf(“control string”,
arg1, arg2,….,argn);
Control string consists of 3 types
of items:
1. Characters that will be printed
on the screen as they appear .
2. Format specifications that define
the o/p format for display of each item.
3. Escape sequence characters such
as \n, \t & \b.
The control string indicates how
many arguments follow & printed according to the specifications of the
control string.
The arguments should match in
number, order and type with format specifications.
Output of
integer numbers:
The format specification for printif
on integer number is:
%wd
Where w specifies the minimum
field width for the o/p d specifies that the value to be printed is an
integer.
The number is written
right–justified in the given field with:
|
||||||||||||||||||||||
Ex:– printf(“%6d”,9876);
|
|
|||||||||||||||||||||
it is possible to force
the printing to be left–justified by placing a minus sign directly after the
%character.
|
||||||||||||||||||||||
Ex:– printf(“%–6d”,9876);
|
|
|||||||||||||||||||||
it is also possible to
pad with zero’s the leading blanks by placing a 0 before the field width specifier.
|
||||||||||||||||||||||
Ex:– printf(“%06d”,9876);
|
|
|||||||||||||||||||||
The minus (–) & zero
are known as flags.
|
||||||||||||||||||||||
Ex:– printf(“%d”,9876);
|
|
|||||||||||||||||||||
Out put of real
numbers:–
The output of a real no
may be displayed in decimal notation using the following format specification
%w.pf
The integer w
indication the minimum number of positions that are to be used for the
display of the value and the integer p indicates the number of digits
to be displayed after the decimal point (precision) f format
specifies.
We can also display a
real no in exponential notation by using the specification.
%w.pe
|
||||||||||||||||||||||
Ex:– y=98.76546
printf(“%7.4f”, y);
|
|
|||||||||||||||||||||
printf(“%7.2f”,y);
|
|
|||||||||||||||||||||
printf(“%-7.2f”, y);
|
|
|||||||||||||||||||||
Printing of a
single character:
A single character can
be displayed in a desired position using the format:
%wc
The character will be
displayed right justified in the field of w columns. We can make the display
left–justified by placing a minus sign before the integer w the minus sign
before the integer w. The defaults value for w is 1.
|
||||||||||||||||||||||
Printing of
strings:
The format specification
for out puting strings is similar to real numbers
%w.ps
Where w specifies
the field width for display and p instructs that only the first P
characters of the string are to be displayed. The display is right–justified.
Ex:
|
||||||||||||||||||||||
%20s
|
|
|||||||||||||||||||||
%20.10s
|
|
|||||||||||||||||||||
Mixed data
o/p:–
Printf uses its control
string to decide how many variables to be printed and what their types are.
Therefore, the format specifications should match the variables in number,
order and type.
If there are not enough
variables or if they are of the wrong type, the o/p results will be
incorrect.
Ex:– printf(“%d%f%s%c”,a,
b, c, d);
|
||||||||||||||||||||||