Write a program to read and display the character using getch() and putch().

#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:

  1. The program starts by including the necessary header files, stdio.h and conio.h.
  2. The main() function is defined.
  3. A character variable ch is declared.
  4. The printf() function is used to display the message “Press any key to continue” on the console.
  5. The getch() function reads a keystroke from the console and assigns it to the variable ch.
  6. The printf() function is used to display the message “You pressed:” on the console.
  7. The putch() function is used to display the character that was read by the getch() function.
  8. 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().

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top