C - SPLessons

Chapter 31

C Input Output

C Input Output

The input and output can also be given with other functions in C language.The below figure shows types of C Input Output functions
Output can also be given with the help of putchar(), puts() functions also.

putchar()

putchar will display a single character at a time on the screen. To print many characters, then use loop statements.

Example: putchar(‘a’);

puts()

puts() will display the string followed by a new line(\n).

Example: puts(splessons)

Input can also be given using the gets() and getchar() functions.

getchar()

getchar()function reads characters one by one until the return() statement is approached. To read all the characters at a time then use loops. It reads the data and stores in a variable.

Example:

int ch;
ch=getchar();

gets()

gets() function reads entire line given by the user through keyboard from stdin file and assigns a null character at the end.

Example: gets(name);

#include <stdio.h>
#include <conio.h>
void main( )
{
	int a;
	printf("Enter a character");
	a=getchar();
	putchar(a);
	getch();
}


 
#include<stdio.h>
void main() 
{ 
	char sp[20]; 
	printf("Enter a string"); 
	gets(sp); 
	puts(sp); 
	getch(); 
}

getch()

getch function scans only one character at a time from the keyboard. The output is not projected on the screen.

Example: 

char c;
c=getch();

getche()

getche function scans only one character at a time from the keyboard. The output is projected to the screen.

Example: 

char c;
c=getche();

putch()

putch function displays only one character at a time to the screen.

Example: 

char c;
c=putch();

#include <stdio.h>
#include <conio.h>
int main()
{
   int i ,j;
   char name[ 10 ] ;
   char o_name[10];
   printf(" Enter name \n ") ;
   for( i=0 ; i <= 10 ; i++ )
   {
    name[ i ] = getch();
    putch(i);
   }
   printf(" Enter other name \n ") ;
   for( j=0 ; j <= 10 ; j++ )
   {
    name[ j ] = getche();
    putch(j);
    }
}

Summary

The chapter “C Input Output” draws out the following main points.

  • putchar(),putch() and puts() are output functions.
  • getchar(),getch(),getche() and gets() are input functions.