C Programming Code Examples
Learn C Language
getch() Function in C Programming Language
getch() Function in C
The getch() is a predefined non-standard function that is defined in conio.h header file. It is mostly used by the Dev C/C++, MS- DOS's compilers like Turbo C to hold the screen until the user passes a single value to exit from the console screen. It can also be used to read a single byte character or string from the keyboard and then print. It does not hold any parameters. It has no buffer area to store the input character in a program.
Syntax for getch() Function in C
#include <conio.h>
int getch(void);
/* wait for any character input from keyboard by getch() function example. */
// C code to illustrate working of
// getch() to accept hidden inputs
#include <conio.h>
#include <dos.h> // delay()
#include <stdio.h>
#include <string.h>
void main()
{
// Taking the password of 8 characters
char pwd[9];
int i;
// To clear the screen
clrscr();
printf("Enter Password: ");
for (i = 0; i < 8; i++) {
// Get the hidden input
// using getch() method
pwd[i] = getch();
// Print * to show that
// a character is entered
printf("*");
}
pwd[i] = '\0';
printf("\n");
// Now the hidden input is stored in pwd[]
// So any operation can be done on it
// Here we are just printing
printf("Entered password: ");
for (i = 0; pwd[i] != '\0'; i++)
printf("%c", pwd[i]);
// Now the console will wait
// for a key to be pressed
getch();
}
Str - str represents the array, in which string is stored. Fpw - FILE pointer to the output file, in which record needs to be written and opening the file FILEW.TXT in "w" mode for
This Code is very simple and a good example of using conditional statement (if-else) in an iteration (i.e. for loop). We shall initiate a for loop having some finite iterations and check
Program display 'every possible combination' of two words from the given 2 string without displaying "repeated combinations". Convert string in 2D array. Make the first string words
C Program to read a text file and convert the file contents in capital (Upper-case) and write the contents in a output file. Program to copy contents of one file into another by changing