C - SPLessons
boy2 Chapter 31

C Input Output

C - SPLessons

C Input Output

shape Description

The input and output can also be given with other functions in C language.The below figure shows types of C Input Output functions

shape Types

shape Alternative
forms

Output can also be given with the help of putchar(), puts() functions also.

putchar()

shape Description

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

Example: putchar(‘a’);

puts()

shape Description

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

Example: puts(splessons)

shape Alternative
forms

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

getchar()

shape Description

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()

shape Description

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);

shape Examples

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


shape Examples

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

getch()

shape Description

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()

shape Description

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()

shape Description

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

Example: 

char c;
c=putch();

shape Examples

#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

shape Key Points

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.