getchar,putchar,getch,putch,getche In C | Programmerdouts


  • 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.
  • C programming language provides many built-in functions to read any given input and to display data on screen when there is a need to output the result.
  • In this tutorial, we will learn about such functions, which can be used in our program to take input from user and to output the result on screen.
  • All these built-in functions are present in C header file "#include<stdio.h>"
  • To use the built in functions we have to specify header files at head in which a particular function is defined(written) while discussing about it.

Character I/O Functions
getchar() & putchar()


getchar() Function:

  • This function is used to Accept one character , from keyboard.
  • It takes Only One Character as an input.

syntax

character_var = getchar();

Example:

#include<stdio.h>            //header file

void main()
{
char a;
printf("Enter a character>");
a=getchar();         //getchar() funtion is  Assigned to variable a 
printf("You Entered  %c ",a);

}

Output:

Enter a character> A
You Entered A


The putchar() Function


  • This function is used to print one character on the screen, from keyboard.

syntax

character_var = getchar();  //accept character


putchar(character_var);   // display the accepted character

EXAMPLE

#include<stdio.h>

character_var = getchar();  //accept character  is 'A'

putchar(character_var);   // it will  display 'A' which is the accepted character

Output:

A



What if we give input more than one character,lets see whats happen when we give input as ABC

#include<stdio.h>

character_var = getchar();  //accept character 

putchar(character_var);   // it will  display 'A' which is the accepted character

Output:

A


Output is the same as above,Because getchar() Function neglect the other characters except of first character




Character I/O Functions
getch() & putch()

getch() Function:


  • This function is used to Accept one character , from keyboard ,without echo to the screen
  • Without echo means when we type non screen it is not visible.
  • This function provides functionality of not getting echo to the screen
  • This Function takes One Character as an input
  • Many programmer use this Function at the end of the program to stop screen until a Character is given as an input.(but its work is not to stop the screen,it Wait's for the character )

syntax

character_var = getch();

Example:

#include<stdio.h>


void main{
char var1;
printf("Enter The Character:");
var1 = getch();        //Not getting echo to the screen

printf("Character  was  %c",var1"); 

}

Output:

Enter the Character:  A
Character was A

putch() Function:


  • This function is used to print one character ,on the screen
  • This Function also print One Character as an input

syntax

putch(variable_name);

Example:

#include<stdio.h>


void main{
char var1;
printf("Enter The Character>");
var1 = getchar();        //using getchar() function 

putch(var1); 

}

Ouput:

Enter the Charater>A
A

Character Input
getche()

getche() Function:


  • This function is used to Accept one character , from keyboard ,echo to the screen
  • This Function also takes One Character as an input

syntax

character_var = getche();

Example:

#include<stdio.h>


void main{
char var1;
printf("Enter The Character:");
var1 = getche();        // echo to the screen

printf("Character  was  %c",var1"); 

}

Output:

Enter the Character: B
Character was B




Further Topics




Related Posts: