catalogue
- 1, Introduction to getchar function
- 2, The getchar function uses
- 3, Clever use of getchar function
- 4, Guess you like it
Zero foundation C/C + + learning route recommendation: C/C + + Learning directory >> Introduction to basic C language
1, Introduction to getchar function
1.getchar principle
getchar function It is used to obtain user input and achieve the purpose of human-computer interaction. When the program calls getchar, the program waits for the user's keyboard keys and stores the characters entered by the user in the keyboard buffer. Until the user presses enter (the enter character is also placed in the buffer), getchar does not start reading characters from the buffer until the user types enter. For example, if the user presses the a key on the keyboard, the getchar function returns the character a;
2.getchar function declaration
This function is declared in the 'stdio.h' header file, and the stdio.h header file should be included when used. For example:
#include<stdio.h> /* *Function Description: gets the input characters of the console user * *Return value: returns the char value corresponding to the user input value */ char getchar(void)
stay C language For console programs, we generally use printf function The output information is displayed in the console window, and the getchar function can obtain the user's input from the console to interact with the user. Pay attention to the difference between the two!
3.getchar usage scenario
1. You can interact with the user to obtain the user's keyboard input. For example, if the user presses the a key on the keyboard, the getchar function returns the character a;
2. For win32 console, getchar function and system("pause") The function is similar to the function to solve the problem that the black screen window of the console flashes;
2, The getchar function uses
/******************************************************************************************/ //@Author: ape programming //@Blog (personal blog address): www.codersrc.com //@File:C language tutorial - C/C++ getchar function //@Time:2021/08/04 08:00 //@Motto: no small steps can lead to thousands of miles. No small streams can lead to rivers and seas. The highlights of program life need to be accumulated unremittingly! /******************************************************************************************/ #include "stdafx.h" #include "stdio.h" #include "windows.h" int _tmain(int argc, _TCHAR* argv[]) { printf("Please enter characters:\n"); char c = getchar(); printf("Get the character you entered:%c\n",c); system("pause"); return 0; } /* Please enter characters: w Get the character you entered: w Please press any key to continue */
Because I input w, the getchar function returns w. you can enter any key; It is worth noting that:
- 1. Corresponding to the character returned by getchar function Placeholder is% c;
- 2. The getchar function can only obtain a single character;
- 3. The Enter key '\ n' is also in the buffer and is taken out by the getchar function as the last character;
If more than one character is entered before pressing enter, all characters will be stored in the buffer` getchar The function returns the first by default character For example, we input 12345 before carriage return. How can we get all the characters in the buffer?
/******************************************************************************************/ //@Author: ape programming //@Blog (personal blog address): www.codersrc.com //@File:C language tutorial - C/C++ getchar function //@Time:2021/08/03 08:00 //@Motto: no small steps can lead to thousands of miles. No small streams can lead to rivers and seas. The highlights of program life need to be accumulated unremittingly! /******************************************************************************************/ #include "stdafx.h" #include "stdio.h" #include "windows.h" int _tmain(int argc, _TCHAR* argv[]) { char c = getchar(); for (;;) //Dead cycle { if (c == '\n') //Check for enter { printf("The last character %c \n", c); break; } else { printf("Get buffer characters %c \n", c); c = getchar(); } } system("pause"); return 0; } /* 12345 Get buffer character 1 Get buffer character 2 Get buffer character 3 Get buffer character 4 Get buffer character 5 The last character Please press any key to continue */
It can be understood from the above code that the Enter '\ n' key is the last character in the buffer;
3, Clever use of getchar function
When the general console program is running, it only sees a black window flash by, and there is no time to see what is displayed. In the previous article, we introduced that this problem can be solved through the system function, and today we can complete this function by using the getchar function, because it is called` getchar After the function, the program waits for user input until press enter, and the program will not continue to execute. The example code is as follows:
/******************************************************************************************/ //@Author: ape programming //@Blog (personal blog address): www.codersrc.com //@File:C language tutorial - C/C++ getchar function //@Time:2021/08/03 08:00 //@Motto: no small steps can lead to thousands of miles. No small streams can lead to rivers and seas. The highlights of program life need to be accumulated unremittingly! /******************************************************************************************/ #include "stdafx.h" #include "stdio.h" int _tmain(int argc, _TCHAR* argv[]) { printf("Ape programming - python and C++course\n"); getchar(); printf("Program end\n"); return 0; } /* Ape programming - python and C + + tutorials */
Running this program, the console outputs the contents of the first printf function: ape programming – python and C++ Tutorial, and the content of the second printf function: the end of the program is not displayed on the console because the getchar function is waiting for us to enter any characters and press enter;
When we press any key on the keyboard and press enter, the program immediately outputs the contents of the second printf function: * * program end * *, and then the main function` main End, the program exits and the console window disappears;
4, Guess you like it
- Difference between C language array subscript out of bounds and memory overflow
- C language uses pointers to traverse arrays
- Difference between C language pointer and array
- Difference between C language pointer array and array pointer
- C language field pointer
- C language function value transfer and address transfer
- C language function indefinite length parameter
- C language function pointer
- C language pointer function
- C language callback function callback
- C language #pragma once
- Difference between C language #include < > and #include ""
- C language const modifier function parameters
- Difference between const and define in C language
- C language # operators
- C language ## operators
- C language__ VA_ARGS__
- C language##__ VA_ARGS__
- C language function indefinite length parameter##__ VA_ARGS__ Classic case
- C language va_start / va_end / va_arg custom printf function
- C language main function
- C language main function parameter main(int argc, char *argv [])
- C language local variables
- C language global variables
- Differences between global variables and local variables in C language
- C language static
- C language extern
- C/C++ putchar function
- C/C++ getchar function
No reprint without permission: Ape programming » C/C++ getchar function