Sie sind auf Seite 1von 8

DualSoft Computers 1

Console I/O Operation

Input / Output System

In C language Input output system is designed to work with a wide verity of input
and output devices.
Input device: Keyboard
Output device: Visual display Unit (VDU), tape drive, disk etc.

I/O system provides an interface between these devices and programmer that is
independent of the actual device being accessed. This interface is known as
stream.

A stream is a sequence of bytes. That act as:

Source: from which the input data can be obtained (Input stream)
Destination: to which output data can be sent.(Output stream)

Diagram: Data streams

Input Stream
Input
device
Extraction from
Input stream

Storage Device Program

Output stream Insertion into


Output output stream
Device

I/O operations are of two types:


• console I/O Functions: These functions receives input from keyboard
and write output to VDU(Visual display unit)
• File oriented I/O operation: These function perform input – output
operation on a secondary storage device.
Deepak Kr. Prajapati
B.E. (Information technology)
Mob.9887036260, 9351670259
DualSoft Computers 2

Input – Output communication

External memory
Write Data DATA files Read Data
( to file ) (From file) File oriented
Input-Output

Internal memory
/ Program
Input function
( Input data from Output functions
Keyboard) (Put data to
Console – Oriented
Input-Output
Console unit
(Screen + Keyboard)

A program involves two types of communication:

1. Data transfer between the console – unit and the program.


2. Data transfer between the program and disk File.

Console Unit

Input device keyboard, output device VDU and program forms a console unit.
• Input data is taken from keyboard.
• Output data shown on VDU (visual display unit).
• Program forms a interface between input and output device. Here inputted
data is processed

Deepak Kr. Prajapati


B.E. (Information technology)
Mob.9887036260, 9351670259
DualSoft Computers 3

VDU

Output stream Program Input stream

Keyboard

Console Unit
Console Input-Output Functions

Console input-output function can be divided in two categories:


• Formatted Input-Output functions
• Unformatted Input-Output Functions

Formatted Unformatted

Type Input Output Type Input Output


char scanf( ) printf( ) char getchar( ) putchar( )
int scanf( ) printf( ) getche( ) putch( )
float scanf( ) printf( ) getch( )
string scanf( ) printf( ) int × ×
float × ×
string gets( ) puts( )

Deepak Kr. Prajapati


B.E. (Information technology)
Mob.9887036260, 9351670259
DualSoft Computers 4

Formatted Input – Output functions

printf( )
It is a standard library function whose declaration and definition are given
in the header file (stdio.h). It is directly connected to the output stream.
Whenever it found any value or message in the list of arguments it will
display them on the screen on the “first come first serve basis”.

Syntax:
printf(“List of arguments”, list of variables);

List of arguments may be:


1. Any Message
2. Back slash code used for following:
a) \n New line
b) \t Horizontal Tab (5 character space)
c) \v Vertical Tab (Only for printers)
d) \b Back space
e) \a Bell alert
f) \f Form feed (Page break)
g) \” To print double quote “
h) \\ To print backslash

3. Conversion specifier – To print the value of any variable according to


its data type.

Specifier Data type


%c Character ‘a’,’ b’
%s Character array (string) “Programming”
%d Integer 5, 10
%f Floating point (Real) 5.2356
%ld Long integer
%lf Double
%u Unsigned
%x Hexadecimal
%o Octal

Deepak Kr. Prajapati


B.E. (Information technology)
Mob.9887036260, 9351670259
DualSoft Computers 5

Example:
int a = 123, b = 12, c = 345;
float d = 3.145, e = 3.1794;

printf(“%d%d%d”, a, b, c);

output: 12312345
Explanation:
In this type of output we cannot differentiate values of a, b and c. for a better
output presentation instead of using %d we use %nd , where n indicates the
number of allocated space to print the value of variable.

Note: If the space required by a number is greater then the allocated space then it
goes into the default allocation of space. That means it got automatically required
space.

printf(“%5d%3d%6d”, a,b,c);

Here number is inserted from right side to left side. [ L  R when we use %nd]
1 2 3 1 2 3 4 5

printf(“%-5d%-3d%-6d”, a, b, c);

Number is inserted from left side to right side. [ L  R when we use %-nd]
1 2 3 1 2 3 4 5

int x = 32221;
printf(“%3d”, x);

3 2 2 2 1

float x = 3.146, y = 56 .3567, z = 12.3;

printf(“%f%f%f”, x, y, z);
output: 3.14600056.35670012.300000

To get better format we can use %m.nf instead of %f.


Where m represents the number of space allocated to represent the value of
variable.
n represents the number of digits after decimal.
Deepak Kr. Prajapati
B.E. (Information technology)
Mob.9887036260, 9351670259
DualSoft Computers 6

printf(“%8.4f%5.2f%5.2f”, x, y, z);

Here number is inserted from right side to left side. [ L  R when we use %nd]
3 . 1 4 6 0 5 6 . 3 7 1 2 . 3 0

printf(“%-8.4d%-5.2d%-5.2d”, a, b, c);

Number is inserted from left side to right side. [ L  R when we use %-nd]
3 . 1 4 6 5 6 . 3 7 1 2 . 3 0

int x = 32221.2;
printf(“%3.2f”, x);

3 2 2 2 1 . 2 0

Deepak Kr. Prajapati


B.E. (Information technology)
Mob.9887036260, 9351670259
DualSoft Computers 7

scanf( )
It is a standard library function whose declaration and definition are
given in the header file (stdio.h). It is directly connected to the input stream.
Whenever it found any value in the list of arguments it will store them into the
variable on the “first come first serve basis”.

scanf(“conversion specifier”, list of variables with prefix & );

Example:
int a;
float b;
char c;
scanf(“%d %f %c”, &a, &b, &c);

Unformatted Input – Output functions

Unformatted console input – output does not allow you to format your input or
output.

Input
Single character
getchar( ), getche( ), getch( )

All three functions above are used to input a single character from keyboard.
Their aim is same but working is different.
syntax:

char c;
c = getchar( ); or getchar(c);
c = getche( ); or getche(c);
c = getch( ); or getch(c);

When we input single character using getchar( )


• Press any character from keyboard. This character will display on
the screen.
• Press enter key to input this character into variable.

Deepak Kr. Prajapati


B.E. (Information technology)
Mob.9887036260, 9351670259
DualSoft Computers 8

When we input single character using getche( )


• Press any character from keyboard. This character will display on the
computer screen and assigned into the variable automatically there is no
need to pressing ENTER key.

When we input single character using getch( )


• Press any character from keyboard. This character will not display on the
computer screen and automatically assigned to the variable. There is no
need to pressing ENTER key.

Output

puchar( ), putch( )

These functions are used to print a single character on the screen.


Example:
putchar(c); or putch(c);

Deepak Kr. Prajapati


B.E. (Information technology)
Mob.9887036260, 9351670259

Das könnte Ihnen auch gefallen