0% found this document useful (0 votes)
34 views

C - ch-4 IO Statements

This document discusses input and output functions in C. It describes formatted and unformatted I/O functions, with scanf() and printf() being the main formatted input and output functions. It provides details on how to use format specifiers in scanf() and printf() to read and write different data types like integers, floats, characters and strings. Additionally, it covers unformatted I/O functions like getchar(), putchar(), gets() and puts().

Uploaded by

Kalpana
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

C - ch-4 IO Statements

This document discusses input and output functions in C. It describes formatted and unformatted I/O functions, with scanf() and printf() being the main formatted input and output functions. It provides details on how to use format specifiers in scanf() and printf() to read and write different data types like integers, floats, characters and strings. Additionally, it covers unformatted I/O functions like getchar(), putchar(), gets() and puts().

Uploaded by

Kalpana
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

Input and Output 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

Formatted scanf() printf()


Unformatted getchar() putchar()
gets() puts()

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

%d Read a decimal integer

%e Read a floating – point number

%f Read a floating – point number

%g Read a floating – point number

%h Read a short int number

%i Read a decimal or hexadecimal or octal


number
%o Read a octal number

%p Read a pointer

%s Read a string

%u Read an unsigned integer

7 %x Read a hexadecimal number


Integer Input
In scanf %d character group is used to
input decimal integer data through
standard input device
Ex:
scanf(“%d”,&number);
The field specification for reading an
integer number is
% w sd
The % indicates that a conversion
specification follows
w is an integer that specifies the field
width of the number to be read
d indicates the datatype character indicates
the number to be read
9
The field specification means the number of
columns reserved for the number to be input.
Ex:
scanf(“%5d”,&number
);

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

 means the specified


%[^characters]
characters are not permitted in the input string
and reading of the string will be terminated at
the encounter of one of these characters

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);

Leading spaces padded with 0


printf(“%05d”,&no) 0 0 1 2 3
18 ;
Floating-point Output

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);

A group of characters (string) can be


displayed to the output device using %s in
printf()
Ex: char bookname[20];

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();

Ex: char name;


name=getchar();

22
Reading a string can be done by using the
function gets()

gets(variable_name);

Ex: char name[10];


gets(name);

23
3.5 Unformatted Output Functions
Writing a single character can be done by
using the function putchar()

putchar(variable_name);

Ex: char name;


name=getchar();
putchar(name);
24
Writing a string can be done by using the
function puts()

puts(variable_name);

Ex: char name[10];


gets(name);
puts(name);
25

You might also like