C - ch-4 IO Statements
C - ch-4 IO Statements
Chapter - 3
1
3.1 Input – Output Functions
C provides a library of functions called
standard input-output library to
perform input-output operations
The header file containing such library
functions is called stdio.h
There are two types of I/O functios
◦ Formatted I/O functions
◦ Unformatted I/O functions
2
TYPE INPUT FUNCTIONS OUTPUT FUNCTIONS
3
3.2 Formatted Input
C provides a scanf() to read the values for
the variables from the Keyboard
It is included in the header file <stdio.h>
The general form is
scanf(“control
string”,address_list);
Where control string specifies the type
of values to be supplied to variables
Address_list specifies the address of
4
locations where data is stored
Rules
The control string must be enclosed
within a pair of double quotes
For every input variable, there must be
one character group
Each character group should begin with a
% and followed by a conversion character
Address_list contains address of input
variables
5
Each input variable must be preceded by
an ampersand(&)
All address_list variables are separated by
commas
Address_list is not enclosed within
double quotes
Values for address_list variables should
match in number, type and order of the
variables
There must be comma to separate the
control string and the address_list
6
Character group Meaning
%c Read a single character
%p Read a pointer
%s Read a string
scanf(“%3d”,&number
If the input number is more than the
);
specified fields then the extra digits are not
stored.
An asterisk symbol between % symbol
and the field specifier is used to skip an
input number without assigning it to its
respective variable.
Ex:
scanf(“%d%*d%d”,&a,&b,&c);
11
Floating-point Input
In scanf %f character group is used to
input float data through standard input
device
Ex:
scanf(“%f”,&number);
For inputting double type numbers
control character %lf must be used.
Ex:
scanf(“%lf”,&number);
13
Character Input
The field specification for reading a string
or character is
% ws or %wc
Some versions of scanf support the
following conversion specification for
strings
%[characters]
%[^characters]
14
%[characters] means that only
characters specified within the brackets are
permissible in the input string and any other
character will result in termination
15
Mixed Mode Input
scanf() can be used to accept more than
one type of data(mixed mode)
The number of input variables, datatype
and their order in control string and
address_list must be same
Ex: scanf(“%d %s
%f”,&id,b_name,&price);
23 C 150.50
16
3.3 Formatted Output
C provides a printf() to write the values
for the variables
It is included in the header file <stdio.h>
The general form is
printf(“control
string”,address_list);
Where control string specifies the type
of values to be supplied to variables
Address_list specifies the address of
17
locations where data is stored
Integer Output
Right justified
printf(“%5d”,&no) 1 2 3
;
Left justified
printf(“%- 1 2 3
5d”,&no);
printf(“%f”,&no); 5 4 . 6 7 4
printf(“%5.2f”,&no 5 4 . 6 7
);
printf(“%5.1f”,&no 5 4 . 6
);
printf(“%- 5 4 . 7
19 5.1f”,&no);
Character Output
A single character can be displayed to the
output device using %c in printf()
Ex: char ch; printf(“%c”,ch);
20
printf(“%s”,bookname);
printf(“%s”,bookname);
P R O G R M M I N G I N c
printf(“%20.10s”,bookname);
P R O G R M M I N
printf(“%-20.10s”,bookname);
P R O G R M M I N
3.4 Unformatted Input Functions
Reading a single character can be done by
using the function getchar()
variable_name = getchar();
22
Reading a string can be done by using the
function gets()
gets(variable_name);
23
3.5 Unformatted Output Functions
Writing a single character can be done by
using the function putchar()
putchar(variable_name);
puts(variable_name);