C (Console I-O)
C (Console I-O)
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.
Source: from which the input data can be obtained (Input stream)
Destination: to which output data can be sent.(Output stream)
Input Stream
Input
device
Extraction from
Input stream
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)
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
VDU
Keyboard
Console Unit
Console Input-Output Functions
Formatted Unformatted
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);
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
printf(“%f%f%f”, x, y, z);
output: 3.14600056.35670012.300000
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
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”.
Example:
int a;
float b;
char c;
scanf(“%d %f %c”, &a, &b, &c);
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);
Output
puchar( ), putch( )