Unit-3 (Input and Output) PDF
Unit-3 (Input and Output) PDF
1
int marks;
………..
scanf("%d", &marks);
…………..
}
In this example, the control string contains only one conversion specification %d, which implies
that one integer value should be entered as input. This entered value will be stored in the variable
marks.
#include<stdio.h>
main ( )
{
char ch;
………
scanf("%c", &ch);
…………
}
Here the control string contains conversion specification character %c, which means that a single
character should be entered as input. This entered value will be stored in the variable ch.
#include<stdio.h>
main ( )
{
float height;
…………..
scanf("%f", &height);
…………….
}
Here the control string contains the conversion specification character %f, which means that a
floating point number should be entered as input. This entered value will be stored in the variable
height.
#include<stdio.h>
main ( )
{
char str[30];
2
……………
scanf("%s", str);
……………
}
In this example control string has conversion specification character %s implying that a string
should be taken as input. Note that the variable str is not preceded by ampersand(&) sign. The
entered string will be stored in the variable str.
Output:
C is excellent
Here control string has only text and no conversion specification character, hence the output is only
text.
#include<stdio.h>
main( )
{
int age;
printf("Enter your age:");
scanf("%d", &age);
}
Here also printf does not contain any conversion specification character and is used to display a
message that tells the user to enter his/her age.
3
#include<stdio.h>
main( )
{
int basic = 2000;
………..
printf("%d", basic);
………….
}
In the above example control string contains a conversion specification character %d, which
implies that an integer value will be displayed. The variable basic has the integer value which will
be displayed as output.
#include<stdio.h>
main( )
{
float height =5.6;
……………………
printf("%f", height);
………………
}
Here control string has conversion specification %f, which means that floating point number will
be displayed. The variable height has that floating point value which will be displayed as output.
#include<stdio.h>
main ( )
{
char ch = '$';
…………
printf("%c", ch);
…………
}
In the above example, the control string has conversion specification character %c, means that
single character will be displayed and variable ch has the character value.
#include<stdio.h>
4
main( )
{
char str[30];
…………..
printf("%s", str);
…………….
}
Here control string has conversion specification character %s, implying that a string will be
displayed and variable name str is a character array, holding the string which will be displayed.
Formatted I/O
Formatted input and output means that data is entered and displayed in a particular format.
Through format specification, better presentation of result can be obtained. Formats for different
specifications are as:
Format for integer input
%wd
Here „d‟ 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 values are not stored correctly. For example
scanf(“%2d%3d”,&a,&b);
i) When input data length is less than the given field width, then the input values are
unaltered and stored in given variables.
Input-
6 39
Result-
6 is stored in a and 39 is stored in b.
ii) When input data length is equal to the given field width, then the input values are
unaltered and stored in given variables.
Input-
27 489
Result-
27 is stored in a and 489 is stored in b.
iii) When input data length is more than the given field width, then the input values are
altered and stored in the variable as-
Input-
278 3479
5
Result-
27 is stored in a and 8 is stored in b and the rest of input is ignored.
Format for integer output
%wd
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);
i) When the length of variable is less than the width specifier.
Input-
78 9
Output-
a = 7 8 , 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 digit, so there are 3
leading blanks.
ii) When the length of the variable is equal to the width specifier.
Input-
263 1941
Output-
a = 2 6 3 , b = 1 9 4 1
iii) When the length of variable is more than the width specifier, then also the output is
printed correctly.
Input-
2691 19412
Output-
a = 2 6 9 1 , b = 1 9 4 1 2
6
scanf(“%3f%4f”,&x,&y);
i) When input data length is less than the given width, values are unaltered and stored in the
variables.
Input-
5 5.9
Result-
5.0 is stored in x and 5.90 is stored in y.
ii) When input data length is equal to the given width, then the given values are unaltered
and stored in the given variables.
Input-
5.3 5.92
Result-
5.3 is stored in x and 5.92 is stored in y.
ii) When input data length is more than the given width then the given values are altered and
stored in the given variables as-
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
%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);
value of variables-
8 5.9
Output:
x = 8 . 0 , y = 5 . 9 0
Value of variables-
7
25.3 1635.92
Output
x = 2 5 . 3 , y = 1 6 3 5 . 9 2
Value of variables-
15.231 65.875948
Output:
x = 1 5 . 2 , y = 6 5 . 8 8
printf(“%.3s”,”programming”); p r o
printf(“%8.3s”,”programming”); p r o