Formatted Input and Output
Formatted input and output means that data is entered and displayed in a particular format. Through format specifications, better presentation of result can be obtained.
Format For Integer Input
%wd :- Here is the conversion specification character for integer value and 'w' is an integer number specifying the maximum field width of input data. if the length of input is more than this maximum field width then the value are not stored correctly. e.g.
scanf("%2d%3d",&a,&b);
"when input data length is less than the given width, then the input values are unaltered and stored in given variable."
Input
6,39
Result
6 is stored in a and 39 is stored in b.
**************************************************************
scanf("%2d%3d",&a,&b);
Input
26, 39
Result
26 is stored in a and 394 is stored in b.
*************************************************************
scanf("%2d%3d",&a,&b);
Input
269 , 3845
Result
26 is stored in a and 9 is stored in b and the rest of input is ignored. the reason of ignoring that %2d and %3d , variable a range of storing the value length is 2 digit and variable b is 3 digits because %wd is responsible for this.
*************************************************************
Format For Integer Output
Here w is the integer number specifying the minimum field width of the output data. if the length of the variable is less than the specified field width, then the variable is right justified with leading blanks.
For example-
printf("a=%3d, b=%4d", a,b);
when the length of variable is less than the width specifier.
Value of variable
78 9
Output
here * is space
a= *78, b=***9
the width specifier of first data is 3 while there are only 2 digits in it, so there is one leading blank. The width specifier of second data is 4 while there is only 1 digits, so there are 3 leading blanks.
******************************************************
- when the length of the variable is equal to the width specifier
value of variable
263 1941
Output
a=263, b=1941
- When length of variable is more than the width specifier, than also the output is printed correctly.
value of variables-
2691 19412
Output
a=2691, b=19412
example-
#include<stdio.h>
main()
{
int a=4000, b=200, c=15;
printf("a=%d\n b=%d\n c=%d\n,a,b,c);
printf("a=%d\n b=%d\n c=%d\n,a,b,c);
}
Output of the first printf would be
a=4000
b=200
c=15
while the second output of prinf
a=4000
b=200
c=15
*******************************************************************************
*******************************************************************************
Format For Floating point Numeric Input-
%wf :- Here 'w' is the integer number specifying the total width of the input data (including the digits before and after decimal and the decimal itself ).
scanf("%3f %4f",&x , &y);
when input data length is less than the given width, value are unaltered and stored in the variables.
Input : 5 , 5.9
Output / result: 5.0 is stored in x and 5.90 is stored in y.
********************************************************
scanf("%3f %4f",&x , &y);
When input data length is equal to the given width, then value are unaltered and stored in the given variables.
Input : 5.3 , 5.92
Result : 5.3 stored in x and 5.92 is stored in y.
*******************************************************
scanf("%3f %4f",&x , &y);
When input data length is more than the given width then the given values are altered and stored in the given variable.
Input : 5.93 , 65.87
Result : 5.9 is stored in x and 3.00 is stored in y.
**************************************************************
**************************************************************
Format For Floating point Numeric Output
**************************************************************
**************************************************************
Format For Floating point Numeric Output
%w.nf :- here w is the integer number specifying the total width of the input data and n is the number of digits to be printed after decimal point. By default 6 digits are printed after the decimal. for example
printf("x=%4.1f, y=%7.2f", x,y);
if the total length of the variable is less than the specified width 'w' then the value is right justified with leading blanks. if the number of digits after decimal is more than 'n' then the digits are rounded off.
Value of variable
a. 8, 5.9
b. 25.3,1635.92
Output :- * used like a blank space
a. x=*8.0, y=***5.90
b. x=25.3, y=1635.92
No comments:
Post a Comment