Sie sind auf Seite 1von 14

Fundamentals of I/O

 Displaying information on screen:


◦ printf()
◦ puts
 Inputting Data:
◦ scanf ()
◦ gets
 Library functions are available for transferring of information
between computer and standard input and output devices. E. g.
keyboard (I/p device) and monitor (o/p device). These are
called console I/O functions.

 These functions are as follows:


◦ printf, scanf, getch, getchar, gets and puts.

 The declaration of these function and related symbolic


constants are available in header file stdio.h
 The following figure shows the classification.
Console I/O functions’

UNFORMATTED
FORMATTED
Scanf
getch , putch , getche
printf getchar , putchar,
gets, puts
 Formatted functions allow us to control where o/p would
appear on screen, number of places after decimal point, spaces
between two values etc.
 printf ( ):This function is used to display output on screen. i.e.
it moves data from computer memory to output device. The
format of the function is:
◦ printf (“format string”, arg1, arg2…) ;
◦ Format string contains the formatting information. Which
consist of general characters which can be displayed as they
are, conversion specifications, and escape sequences. arg1,
arg2… are individual o/p data items. They can be constants,
variables (of any data type), array names or even constant
expressions.
# include<stdio.h>
void main()
{
int avg = 345 ;
float per = 69.2 ;
char grade = 'B' ;
printf ("Average = %d \n percentage = %f \n Grade = %c",
avg, per, grade) ;
}
 scanf( ): Data can be entered into the computer from an input
device (like keyboard) using the function scanf.
◦ The format of the function is:
scanf (“format string”, &arg1, &arg2 …) ;
 The format strings consist of the conversion specifier.
 The arguments can be variables or array name and should
represent address of variables, where the data item is to be
stored i.e. each variable must be preceded by an ampersand
(&). However array names should not begin with an
ampersand. For the time being it is sufficient to know that if
we have a variable i, its address is &i.
 E . g.
{
int avg ;
float per ;
char grade ;
scanf (“%d %f %c” , &avg, &per ,&grade) ;
}
 The following are the unformatted function used to input and
output characters.
 getch( ): It is used to input a single character. It will instantly
read the character and does not require enter key to be pressed.
It returns the character typed but does not echo it on the
screen.
 E.g.
int getch (void) ;
ch = getch();
◦ ch is assigned the character returned by getch.
 putch(): It is the counterpart of getch. i. e. it displays a single
character on the screen. It return the character displayed
◦ E.g.
int putch (int) ;
putch(ch) ;
◦ ch => the character to be printed.
 getchar():It is used to input a single character. It requires enter
key to be passed following the character that you typed. It
echoes the character that you entered.
Syntax : ch = getchar ;
 putchar(): It is other side of getchar. It displays a single
character on the screen.
Syntax: putchar(ch) ;
◦ The above functions are alternative to scanf and printf used
for inputting and outputting characters.
◦ These character I/O function can be used to input and output
string by reading or writing one character at a time inside a
multipass loop.
◦ There are functions available for inputting and outputting
string directly. Strings are sequence of character that is
terminated with ‘\0’ character, which is called NULL
character.
 gets() and puts() : They facilitate transfer of string between
computer and standard input-output devices.
◦ They accept single argument. Argument must be data item that
represent a string. The string may include white space characters.
◦ When using gets, enter key has to be pressed to end the string.
The gets and puts functions offer simple alternative to use of
scanf and printf for reading and displaying string as illustrated.
◦ E. g.
#include<stdio.h>
main ( )
{
char line [80] ;
gets (line) ;
puts (line) ;
}
 %d, %f, %c are called conversion specifiers. They tell how the
arguments are to be displayed.
 That is ‘avg’ to be displayed as an integer, ‘per’ as float and
‘grade’ as character.
 Field width specifiers can accompany conversion specifiers.
Also whether the value is to be left justified or right justified
can be mentioned.
 Following is the table, which gives all the conversion
specifiers.
Conversion Character Meaning
c Data item is a single character
d Data item is a decimal integer
e Data item is a floating-point value
f Data item is a floating-point value
g Data item is a floating-point value
h Data item is a short integer
i Data item is a decimal, hexadecimal or octal integer
o Data item is an octal integer
s Data item is a string followed by a white space
character (the null character \0 will automatically be
added at the end)
u Data item is an unsigned decimal integer
x Data item is a hexadecimal integer
[…..] Data item is a string, which may include white space
characters

Das könnte Ihnen auch gefallen