Input Output in C language:
Accepting the data from other sources into the program is
called input.
Sending data from the program to other sources is called
output.
I/O operations in C language is classified into
- Console
I/O (Keyboard and monitor)
- File
I/O
- Network
I/O
C language provides number of pre-defined function to
perform input and output operations.
These functions are classified into formatted and
unformatted functions.
Formatted functions are capable to present the data in
required format using different data formatting techniques.
Console I/O:
C language provides number of library functions to perform
console I/O operations.
Here keyboard is console input and monitor is console
output.
These functions are defined within the header file
"stdio.h"
1. printf():
It is the function used to present the data on to the
console output device (Monitor)
It is the formatted output function used to present the data
on to the monitor in a required format using formatting characters and escape sequences.
It is discoursed in length at the beginning of tutorial.
2. scanf():
It is the function used to read the data from the console
input device (Kayboard) into the specified variables.
It is the formatted input function.
3. sprintf():
It is the formatted output function used to write the data
on to the string rather monitor.
In the example1 printed the formatted output using printf()
In the example2 printed the formatted output using sprintf()
by storing onto a string
Example1:
#include<stdio.h>
void main()
{
char str[150];
int empno=1005;
char
name[20]="Mr.black";
char
place[20]="kavali";
long int
pin=524201;
clrscr();
printf("empno:%d\nName:%s\nPlace:%s\nPin:%ld",empno,name,place,pin);
getch();
}
Output:
empno:1005
Name:Mr.black
Place:kavali
Pin:524201
Example explained:
In the above example the printf() function prints the
formatted data onto the monitor directly
Example 2:
#include<stdio.h>
void main()
{
char str[150];
int empno=1005;
char
name[20]="Mr.black";
char
place[20]="kavali";
long int
pin=524201;
clrscr();
sprintf(str,"empno:%d\nName:%s\nPlace:%s\nPin:%ld",empno,name,place,pin);
printf("%s",str);
getch();
}
Output:
empno:1005
Name:Mr.black
Place:kavali
Pin:524201
Example explained:
In the above example the sprintf() function prints the
formatted data onto the a string "str" which is again printed onto
the monitor.
4. sscanf():
It is the formatted input function used to read the data
from a complex string into the individual variables.
Example:
#include<stdio.h>
void main()
{
char
str[150]="1002 Mr.Black Kavali 524201";
int empno;
char name[20];
char place[20];
long int pin;
clrscr();
sscanf(str,"%d%s%s%ld",&empno,name,place,&pin);
printf("empno:%d",empno);
printf("\nName:%s",name);
printf("\nPlace:%s",place);
printf("\nPin
code:%ld",pin);
getch();
}
Output:
empno:1002
Name:Mr.Black
Place:Kavali
Pin code:524201
Example explained:
Here the string "str" has details of an employee.
sscanf() read the details from "str" in a sequence
and store into variables.
5. gets():
It is the function used to accept a line of text from the
keyboard.
It accepts the reference of character array as argument.
6. puts():
It is an unformatted function used to write a line of text
on to the monitor.
Example:
#include<stdio.h>
void main()
{
char x[100];
clrscr();
printf("Enter
a line of text:");
gets(x);
printf("The
given text is:");
puts(x);
getch();
}
Execution:
Enter a line of text: hello world
The given text is: hello world
getchar(), getche(), getch():
All these functions are used to accept a single character
from the keyboard and store into the variable.
7. getchar():
It is the function used to accept a character from the
keyboard.
It echoes the character given by the user.
It needs user confirmation.
Example:
#include<stdio.h>
void main()
{
char ch;
clrscr();
printf("Enter
a character:");
ch=getchar();
printf("The
given character is %c",ch);
getch();
}
Example explained:
The given character "s" is echoed (Displayed)
Character is accepted on pressing enter or carriage return.
8. getche():
It is similar to getchar()
It echoes the character given by the user.
It doesn't need user confirmation.
Example:
#include<stdio.h>
void main()
{
char ch;
clrscr();
printf("Enter
a character:");
ch=getche();
printf("The
given character is %c",ch);
getch();
}
Execution: Enter a character : s The given character is s
Example explained:
The given character "s" is echoed (Displayed)
Character is accepted without user confirmation.
9. getch():
It is similar to getchar()
It doesn't echo the character given by the user.
It doesn't need user confirmation.
Example:
#include<stdio.h>
void main()
{
char ch;
clrscr();
printf("Enter
a character:");
ch=getch();
printf("The
given character is %c",ch);
getch();
}
Execution: Enter a character : The given character is s
Example explained:
The given character "s" is not echoed (Displayed)
Character is accepted without user confirmation.
10. putchar(), putch():
These are the functions used to print a character on the
monitor
There is no difference among the putchar() and putch() in
their functionality.
example:
#include<stdio.h>
void main()
{
char ch='a';
clrscr();
putchar(ch);
getch();
}
Output:
a
File I/O:
0 comments:
Post a Comment