Quick links: C-Tricks HR Interview Que. Code Snippets C Programs C++ Programs Java Programs PHP Examples

Home » C programming language

Single Character Input and Output using getch(), getche(), getchar(), putchar() and putch()

getch()

This function is used to get (read) single character from standard input device (keyboard) without echoing i.e. it does not display the input character & it does not require [return] key after input. getch() is declared in conio.h header file.

/*Compatible for TurboC compiler  */
#include <stdio.h>
#include <conio.h>

int main()
{	
	char ch;
	
	printf("Enter a character :");
	ch=getch();
	
	printf("\nEntered character is : %c",ch);
	return 0;
}

Output

Enter a character:
Entered character is: G

Here, input character is G, which is not display while giving input.


Sponsored Links



getche()

This function is used to get (read) single character from standard input device (keyboard) with echoing i.e. it displays the input character & it does not require [return] key after input. getche() is declared in conio.h header file.

/*Compatible for TurboC compiler  */
#include <stdio.h>
#include <conio.h>

int main()
{
	char ch;
	printf("Enter a character :");

	ch=getche();
	printf("\nEntered character is : %c",ch);

	return 0;
}

Output

Enter a character: G
Entered character is: G

Here, input character is G, which displays character while giving input and does not require [return] after pressing ‘G’.

getchar()

This function is used to get (read) single character from standard input device (keyboard) with echoing i.e. it displays the input character & require [return] key after input. getchar() is declared in stdio.h header file.

#include <stdio.h>

int main()
{
	char ch;

	printf("Enter a character :");
	ch=getchar();
	printf("\nEntered character is : %c",ch);

	return 0;
}

Output

Enter a character: G
Entered character is: G

Here, input character is G, which displays character while giving input and after pressing [return] key, program’s execution will move to next statement.

putchar() & putch()

These functions are used to put (print) a single character on standard output device (monitor).

#include <stdio.h>
int main()
{
	char ch;

	printf("Enter a character :");
	ch=getchar();
	printf("\nEntered character is :");
	putchar(ch);

	return 0;
}

Output

Enter a character: G
Entered character is: G
Solved programs


Sponsored Links


Most viewed pages


Featured posts
C Tips and Tricks

Keywords (like Linux, Java, Oracle etc) used in this website are the trademarks of their respective owners/company. This website is not affiliated to any company. The tutorials are designed by includehelp Team with the help of well known books and other online resources. These Tutorials are free for all, for learning purposes only. Enjoy learning:)
Copyright © 2015 - 2016. All Rights Reserved.