Two, Putchar (), Getch (), Getche (), and GetChar () functions
1. Putchar () function
Putchar () function is to output a character to a standard output device in the form of: Putchar (CH);
Where Ch is a character variable or constant. The
Putchar () function is equivalent to printf ("%c", ch);
Example 6:
#include <stdio.h>
Main ()
{
Char c: /* define character variable/
c= ' B '; /* assign value to character variable/
Putchar (c); /* output the character */
Putchar (' \x42 '); /* output Letter b*/
Putchar (0x42); /* output the letter b*/
} directly with ASCII code value The
output function statements from four consecutive characters in this example can distinguish different assignment methods for character variables.
2. Getch (), Getche (), and GetChar () functions
(1) getch () and Getche () functions
Both functions read a character from the keyboard. The calling format is:
Getch ();
Getche (); The difference between the
is that the Getch () function does not echo the read Word to the display screen, while the Getche () function echo the read Word to the display screen.
Example 7:
#include <stdio.h>
Main ()
{
char c, ch;
C=getch (); /* reads a character from the keyboard without echoing back to the character variable c*/
Putchar (c); /* output the character/
Ch=getche (); /* A character is sent back from the keyboard to the character variable ch*/
Putchar (CH);
}
uses the features of ECHO and no echo, which are often used to perform functions such as pausing during interactive input.
Example 8:
#include <stdio.h>
Main ()
{
Char c, s[20];
printf ("Name:");
Gets (s);
printf ("Press any key to confinue ...");
Getch (); /* Wait to enter any key
}
(2) GetChar () function
The GetChar () function also reads a character from the keyboard and brings it back. It differs from the previous two functions in that the GetChar () function waits for input until it is pressed to enter, and all input characters are displayed on the screen one by one before the carriage return. But only the first character is the return value of the function.
The call format for the GetChar () function is:
GetChar ();
Example 9:
#include <stdio.h>
Main ()
{
char c;
C=getchar (); /* Read the character from the keyboard until the carriage return is over
Putchar (c); /* Display the first character of the input/*
Getch (); * * Waiting to press Ning * *
}