Bca C102 SM03
Bca C102 SM03
operations
• I/O operations are helpful for a program to interact with users. C stdlib is the standard C library for input-
output operations. Two essential streams play their role when dealing with input-output operations in C. These
are:
• 1. Standard Input (stdin)
• 2. Standard Output (stdout)
• • Standard input or stdin is used for taking input from devices such as the keyboard as a data stream.
• • Standard output or stdout is used to give output to a device such as a monitor.
• READING A CHARACTER
• The easiest and simplest of all I/O operations are taking a character as input by reading that character from
standard input (keyboard). getchar() function can be used to read a single character. This function is an
alternative to scanf() function.
• Example - var_name = getchar();
WRITING A CHARACTER
• Like getchar(), there is another function that is used to write characters, but one at a time.
• Example - putchar(var_name);
• FORMATTED INPUT
•
• It refers to input data that has been arranged in a specific format. This is possible in C using scanf() function.
We have already encountered this and are familiar with this function.
• Example :-
• void main()
• {
• int var1= 60;
• int var2= 1234;
• scanf("%2d %5d", &var1, &var2);
• }
• Here the % sign denotes the conversion specification; 2,5 signifies the integer number that defines the field
width of the number to be read. d defines the number to be read in integer format.
• C Input and Output
• Input means to provide the program with some data to be used in
the program.
• Output means to display data on screen or write the data to a
printer or a file.
• Single character input and output[getchar( ) and putchar()]
• input- getchar()
• output- putchar()
• The int getchar(void) function reads the next available character
from the screen and returns it as an integer. This function reads
only single character at a time.
• The int putchar(int c) function puts the passed character on the
screen and returns the same character. This function puts only
single character at a time.
Unformatted Input and Output
• Unformatted input output function do not contain format specifier in their syntax so it can’t format data.
• It only works with char data type and string(character Array) and not with any other type.
• Unformatted input output functions are the basic form of i/o and they do not input or display output in user
desired format.
• program
• #include<stdio.h>
• int main( )
•{
• int d;
• printf( "Enter a value :");
• d = getchar( );
• printf( "\nYou entered: ");
• putchar( d );
• return 0;
•}
Input Output Functions in C
Standard input : the term standard input refers to the input using keyboard. A program may need certain input
from the user. Input functions are those function which is used to take the input from the user.
C language provide many functions to get input from the user.
1. scanf()
2. gets()
3. getch()
4. getche()
1. gets() function:- is declared in stdio.h header file.
It is used to input a string (character array ) from standard input.
#include<stdio.h>
#include<conio.h>
void main()
{
char c[10];
gets(c);
printf(“%s”,c);
}
• #include<stdio.h>
• int main( )
• {
• char str[100];
• printf( "Enter a value :");
• gets( str );
• printf( "\nYou entered: ");
• puts( str );
• return 0;
• }
String input and output gets() and puts()
Input--- gets (str)
Output---puts (str)
The gets( ) function reads a line from stdin into the buffer pointed to
by s until either a terminating newline or EOF (End of File).
The puts( ) function writes the string 's' and 'a' trailing newline to
stdout.
2. getchar():-this function is declared in stdio.h header file. getchar() function ready only single character at a
time from standard input.
Example of getchar() function
#include<stdio.h>
#include<conio.h>
void main()
{
char c;
c=getchar();
printf(“%c”, c);
}
3. getch() & getche() function:- both functions are declared in conio.h
header file . Both function reads a single character.
getch() functions is used to hold the screen.
Difference between is getch() don’t echo on the console screen but
getche() echos before display output.
#include<stdio.h>
#include<conio.h>
void main()
{
char c;
c=getch(); // don’t show c=getche();
printf(“%c”,c);
}
Standard output:-the term standard output refers to the output displayed on the monitor. Output functions are
those function which is used to print message or value of variable on output screen.
C language provides many functions to display program output to the user.
1. printf()
2. puts()
3. putchar()
4. putch()
1. puts() function:- this function is declared in stdio.h header file.
It is used to display a string ( character array) from the standard output.
#include<stdio.h>
#include<conio.h>
void main()
{
char c[10];
gets(c);
puts(c);
}
2. putchar() function:-this function is declared in stdio.h header file.
Putchar() function displays only single character at at time from standard output.
#include<stdio.h>
#include<conio.h>
void main()
{
char c;
c=getchar(); //required to output from enter keyword
putchar(c);
}
3. putch() function:-this function is declared in conio.h header file.
putch() functions is used to display a single character.
#include<stdio.h>
#include<conio.h>
void main()
{
char c;
c=getch();
putch(c);
}
Formatted Functions (printf(), Unformatted Functions (getchar(),
Feature
scanf(), etc.) puts(), etc.)