#include<stdio.h>
#include<conio.h>
int main()
{
char ch;
printf("Press any key to continue");
ch=getch();
printf("\n You pressed:");
putch(ch);
}
/*The fuction getch() reads a keystroke and assigns to the variables ch.The putch() displays the
character pressed.*/
This program reads a single character from the user and then displays the character on the console using the getch()
and putch()
functions respectively. The getch()
function reads a keystroke from the console, but does not echo the character to the screen, whereas putch()
displays the character on the screen.
Here’s a breakdown of the program:
- The program starts by including the necessary header files,
stdio.h
andconio.h
. - The
main()
function is defined. - A character variable
ch
is declared. - The
printf()
function is used to display the message “Press any key to continue” on the console. - The
getch()
function reads a keystroke from the console and assigns it to the variablech
. - The
printf()
function is used to display the message “You pressed:” on the console. - The
putch()
function is used to display the character that was read by thegetch()
function. - The program ends by returning 0.
Note that this program is using the conio.h
library, which is not part of the standard C library, and may not be available on all platforms.
Thanks
Write a program to read and display the character using getch() and putch().