Last update Sep 14, 2002
Function | Description |
---|---|
_kbhit | Determines if a keyboard key was pressed. |
_ungetch | Puts a character back into the keyboard buffer. |
_getch | Reads a character directly from the console, without echo. |
_getche | Reads a character directly from the console, with echo. |
_putch | Writes a character directly to the console. |
_cgets | Gets a string directly from the console. |
_cprintf | Formats and prints a string directly to the console. |
_cputs | Outputs a string directly to the console. |
_cscanf | Reads and formats values directly from the console. |
/* Example of _kbhit() */ #include <stdio.h> #include <conio.h> #include <stdlib.h> int main() { printf("Hit any character key when ready\n"); while (!_kbhit()) ; printf("\nThe key pressed was (%c)\n", _getch()); return 0; }
Hit any character key when ready The key pressed was (b)
/* Example for _ungetch */ #include <stdio.h> #include <conio.h> #include <string.h> #include <ctype.h> int main() { int i = 0; char c; char str[128]; printf("Enter an integer: "); while ((c = _getche()) != EOF && isdigit(c)) i = (i * 10) + (c - 48); if ((c != EOF) && (c != '\r')) ungetch(c); if (c != '\r') { gets(str); printf("The integer is %d, and the rest is \"%s\"\n", i, str); } else printf("\nThe integer is %d", i); return 0; }
/* Example for _getch */ #include <stdio.h> #include <stdlib.h> int main() { int input; printf(" Press charcter key..."); input = _getch(); printf("\n'%c' was returned by _getch()\n", input); return 0; }
/* Example for _getche */ #include <stdio.h> #include <stdlib.h> int main() { int input; printf(" Input a character: "); input = _getche(); printf("\n'%c' was returned by _getche()\n", input); return 0; }
/* Example of _putch */ #include <stdio.h> #include <conio.h> int main() { char ch = 0; printf(" Input a string: "); while ((ch != '\r')) { ch = _getch(); _putch(ch); } return 0; }
/* Example for _cgets Also demonstrates _cprintf _CGETS. C */ #include <conio.h> int main() { char buffer[22], *return_value; buffer[0] = sizeof (buffer) -2; _cprintf(" Type something: "); return_value = _cgets (buffer); _cprintf("\n\rsize = %d\n\rbuffer = '%s'\n\rreturn value = '%s'\n\r", buffer[1], buffer + 2, return_value); return 0; }
/* Example for _cprintf _CPRINTF. C */ #include <conio.h> int main() { _cprintf(" 1. \\n works different for console I/ O, it goes down:\n"); _getch (); _cprintf(" 2. \\r goes back:\r"); _getch (); _cprintf(" 3. \\r\\n goes down and back:\r\n"); _getch (); _cprintf(" 4. Like this."); return 0; }
/* Example for _cputs. c SAY. C */ #includeint main(int argc, char *argv[]) { int i; for (i = 1; i < argc; i += 1) { _cputs (argv[i]); _cputs (" "); } return 0; }
/* Example for _cscanf Also demonstrates _cprintf */ #include <conio.h> int main() { int a_number; char a_string[20]; _cprintf("Enter a number, then a string\n\r"); _cscanf("%d %s", &a_number, a_string); _cprintf("\nThe number was %d, the string was '%s'\n", a_number, a_string); return 0; }
Enter a number, then a string 1492 Ambidexterous The number was 1492, the string was 'Ambidexterous'