GetChar () putchar (CH) scanf () header file stdio.h
Getch () Getche () header file conio.h
Gets () puts () header file Stdio.h
both the Getch () and the Getche () functions read a character from the keyboard, However, the getch () function does not echo the read-in character on the display screen, and the Getche () function echo the read-in Word to the display screen.
The difference between the GetChar () function and the above two functions is that you wait for input until you press enter to end, and all input characters before carriage return are displayed on the screen one at a- but only the first character is the return value of the function .
GetChar () header file Stdio.h
Prototype: int getchar (void); is a library function in stdio.h that returns the number of an int
The function is to read a character from the stdin stream, that is, if the stdin has data, you can read it directly without entering it, the first time GetChar () does require manual input, but if you lose more than one character, the later GetChar () When executed, it is read directly from the buffer.
There is a return value of type int. When the program calls GetChar. The program waits for the user to press the key. The characters entered by the user are stored in the keyboard buffer until the user presses ENTER (the carriage return character is also placed in the buffer). When the user enters Enter, GetChar begins to read one character at a time from the stdio stream. The return value of the GetChar function is the ASCII code of the first character entered by the user, such as an error of 1.
If the user entered more than one character before pressing enter, the other characters inode remain in the keyboard buffer, waiting for subsequent getchar calls to be read. Note: Characters that have been in the keyboard buffers, do not wait for the user to press ENTER to call GetChar until the characters in the buffer are read as Just wait for the user to press the key.
Keyboard input characters are stored in the buffer, once you type Enter, GetChar into the buffer to read the characters, only return the first character as the value of the GetChar function at a time, if there is a loop or enough GetChar statement, it will read out all the characters in the buffer until ' \ n '. To understand this, the sequence of characters you enter is read sequentially because the function of the loop is to reuse GetChar to read the characters in the buffer, instead of GetChar to read multiple characters, in fact GetChar can only read one character at a time
Ch=getchar (); Wait for a character to be entered from the keyboard
Putchar (CH); Output this character
#include <stdio.h>int main () {int C; while ' \ n ' // continually loops through calls to GetChar until the user types a carriage return (that is, newline)printf ("%c", c); return 0 ;}
Results
Getch () non-standard function, be aware of the porting header file: Conio.h
is a non-echo function, when the user presses a character, the function automatically reads, no need to press ENTER, some C command-line program will use this function to do the game,
Function prototype: int getch (void); Reads a character from the console, but does not display char ch on the screen, or int ch;_getch (), or ch=_getch (); _getch (); Will wait for you to press any key before continuing to execute the following statement; Ch=_getch (); Will wait for you to press any key, the ASCII code corresponding to the key character assigned to CH, and then execute the following statement.
#include <stdio.h>#include<conio.h>int main () { char C; while ((C=getch ())! ='\ r'// one character typed by the user is output one * until the ENTER key is pressed. printf ("*"); } return 0 ;}
Results:
Getche () function: Take character from console immediately after input, not end with return (with Echo)
Prototype: int getche (void);
//Header file: conio.h return value: ASCII code corresponding to the input character
#include <stdio.h>#include<conio.h>int main (void) { Char ch; printf ("pleaseinput a character:"); ch=getche (); printf ("\nyou" has input a character '%c ' \ n", ch ); return 0 ;}
Results: Visible, 1, with Echo 2, once entered a word Fu Ze immediately read back from the console
The difference between the Getch () and the Getche () functions:
#include <stdio.h><conio.h>int main () { char c, ch; C// read a character from the keyboard without echoing to the character variable C // output The character puts (""/ /puts () outputs the contents and wraps // reads a character from the keyboard to the character variable ch putchar (CH); return 0 ;}
For Ch=getche (), the running result is visible that both the characters typed by the user are echoed, and the characters are immediately taken from the console and output by Putchar (CH), so you see two duplicate characters
gets () reads a line, reading the string from the stream until a newline character or ' \ n ' reads to the end of the file, and ends with ' + ' as the string. The string being read is temporarily present in the given argument string
Prototype: char * gets (char * string); Note You can only receive the char* type, and a pointer to the char* type succeeds, otherwise null is returned
#include <stdio.h>int main (void) { char str[]; printf ("Input a string.\n"); Gets (str); printf ("Thestring you are input is:%s", str); // output All values, note a}
Note: Because get () does not check the size of the string, you must encounter a newline character or end of file to end the input, so it is easy to create a security problem with cache overflow, which causes the program to crash, you can use fgets () instead
If you enter 123456 (the length is less than 10), the output is:
Input a string.
123456
The string you input is:123456
If you enter 12345678901234567890 (longer than 10), the output is:
Input a string.
12345678901234567890
The string you input is:12345678901234567890
Also see the system prompt program has crashed.
scanf () header file: stdio.h
Prototype: scanf ("Format control string", variable address list);
When a string is accepted:scanf ("%s", character array name or pointer), that is, the second argument can only be a character pointer , and if an array of type int is accepted, it is necessary to loop continuously, taking a number in the array each time
#include <stdio.h>intMain () {intarr[5]; for(intI=0; i<5; ++i) {scanf ("%d", &arr[i]);//Note that in the first parameter of scanf, it is best not to add any other characters, such as spaces, tabs, etc. except in the format control starting with% } /*//You can also replace the for (auto& Elem:arr) {scanf ("%d", &elem) with the c++11 attribute equivalent; } */ for(intj=0;j<5; ++j) {printf ("%d", Arr[j]); } return 0;}
Note: In the first parameter of scanf, in addition to the format control starting with%, it is best not to add any other characters, such as spaces, tabs, etc., as the user adds a space at the time of entry, tab or line breaks are recognizable, rather than consistent with formatting controls in scanf.
The puts () function is used to write a string to a standard output device (screen) and wrap it in a way that is called, puts (s), where S is a string character (string array name or string pointer), and note that the puts is encountered when outputting a string (that is, the character Terminator) to stop
Prototype:
int puts (const char *string), such as puts ("*******************************");
GetChar () Getch () Getche () The use and difference of puts () scanf ()