Differences and relationships between scanf (), gets (), and getchar () functions in C Language

Source: Internet
Author: User

Differences and relationships between scanf (), gets (), and getchar () functions in C Language
As we all know, scanf and gets are functions that input data from the keyboard. The basic function functions are not described in detail here. They only analyze the error points of the two functions in depth.

Int main () {char a [20]; char B [20]; char c [20]; printf ("enter a string \ n "); scanf ("% s", & a); printf ("Enter B string \ n"); gets (B); printf ("Enter c string \ n "); gets (c); return 0 ;}

 

[Analysis] the program is intended to input three strings. The reason why printf is used to prompt the input is to better understand how the program works, you can see from the running interface that the program prompts to enter string a and string c, but it will pass string B without input. This is confusing. The scanf function does not read or convert strings when it encounters carriage return, spaces, or tabs, and discards them and the characters following them to the buffer zone, add '\ 0' to the character to be read '. so when gets (B) is reached, it will read the rest of the buffer and the last line break will end, so the program will not prompt to enter the string B, however, from monitoring, we can see that B actually reads strings. Then, the buffer zone does not have the remaining characters gets (c), which can be used to input a c string. In addition, the gets function can be used to display strings with spaces. Here is another note: gets (c) reads the characters entered from the keyboard and the carriage return characters that need to be refreshed in the buffer zone. Unlike scanf, it does not discard the carriage return to the buffer, but discards the linefeed and changes it to the string ending sign '\ 0 '. Therefore, we generally like to use this input function with the final clearance. At the last point, we can see that string B actually reads junk data in the buffer zone. Therefore, to avoid the impact of junk data in the input stream buffer on subsequent reading, you need to clear the buffer. The following describes the methods (different platforms). C Standard stipulates that the fflush () function is used to refresh the output (stdout) cache. It is not defined for input (stdin. However, some compilers also define the implementation of fflush (stdin), such as Microsoft's VC. Whether other compilers also define the manual for fflush (stdin) implementation to find it. The GCC compiler does not define its implementation, so fflush (stdin) cannot be used to refresh the input cache. For compilers that do not define fflush (stdin), you can use the fgets () function to replace it (it is better to use functions such as getchar () and scanf ). You can ignore other input such as carriage return left in the input stream so that the next input is always in a "clean" state. (This is acceptable for any platform.) the scanf function reads characters.
int main(){    char ch1;    char ch2;    char ch3;    char ch4;    scanf("%c",&ch1);    scanf("%c",&ch2);    scanf("%c",&ch3);    scanf("%c",&ch4);    return 0;}

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.