Usage of puts () putc () getchar () gets () getch () getche () in C language, putcgetche
1. puts () Output string, stops when '\ 0' is encountered, and converts' \ 0' into a carriage return line break
Eg:
#include
int main(){ char a[] = "this is an example"; puts(a); return 0;}
It is also very understandable that the character string ends with '\ 0', so the output result of the above example will end with a line break. Note the following example:
Eg:
# Include
Int main () {int I; char a [20]; for (I = 0; I <20; I ++) {a [I] = 'B ';} // stop when '\ 0' is encountered, and wrap a [10] =' \ 0'; puts (a); return 0 ;}
In the above example, only 10 B is output, and a line break occurs because '\ 0' is encountered'
2. putc () is also output. It can be used for character arrays. However, only one character is output.
Eg:
#include
int main(){ char a[] = "hello world\n"; int i = 0; putc(a[i],stdout); printf("\n"); while(a[i]){ putc(a[i++],stdout); } return 0;}
In the above example, the first putc outputs the character h, and the second putc outputs the hello world line feed.
3. getchar () can be a string of characters, but only the first character is returned.
Eg:
# Include
Int main () {char ch; ch = getchar (); printf ("the input character is: % c", ch); return 0 ;}
After running the preceding example, enter abc and press Enter. The value of ch is only a, and only a is displayed on the screen.
4. gets () input a string to the character array from the terminal and obtain a function value. The function value is the starting address of the character array, the gets function is generally used to input a string to the character array, regardless of the function value.
Eg:
# Include
Int main () {char a [20]; gets (a); printf ("the input string is % s \ n", );}
* 5. The getch () and getche () functions are used to enter a single character. The input of the former is not displayed on the screen, and the characters entered by the latter are displayed on the screen.
Note: add the header file conio. h to the two functions.
Getch ()
Eg:
# Include
# Include
Int main () {char ch; ch = getch (); printf ("the input character is: % c", ch); return 0 ;}
Getche ()
Eg:
# Include
# Include
Int main () {char ch; ch = getche (); printf ("the input character is: % c", ch); return 0 ;}
Well, run the code on your own and you will understand it.