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.

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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL







Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.