Comparison between scanf (), gets (), and fgets:

Source: Internet
Author: User

Gets () stops reading strings from the stdin stream until it receives a linefeed or EOF, and stores the read results in the character array pointed to by the buffer pointer. The linefeed is not used as the content of the read string. The read linefeed is converted to a null value and ends the string.

 

Note: The gets function can be read infinitely without determining the upper limit. Therefore, the programmer should ensure that the buffer space is large enough to avoid overflow during read operations. If overflow occurs, the extra characters are written into the stack, which overwrites the original content of the stack and destroys the values of one or more irrelevant variables. To avoid this situation, we can use fgets () to replace gets () (in Linux, gets () is used in the program, and the compiling will contain warning: the 'gets' function is dangerous and shoshould not be used ).

 

Scanf ("% s", & S) and gets (s) functions are similar, but not identical. Use scanf ("% s", & S) A problem occurs when the function inputs a string. If a space is entered, the string ends and the character after the space is processed as the next input, but gets () the function will receive the entire input string until a line break occurs.

 

The fgets function can be used to read strings from a file or input a string from the screen. The fgets function is called in the following format: fgets (STR, N, FP); here, FP is a file pointer (this parameter is written as stdin to get words from the screen ); STR is the starting address of the string. N is an int type variable, which indicates the maximum number of characters read from the string, the function is to read n-1 characters from the file indicated by FP into the space where STR is the starting address. If it is not fully read by n-1 characters, if you have read a linefeed or an EOF (End mark of the file), The read operation ends. The string to be read contains the linefeed (that is why the second parameter is n, note the following code:

Int Len;

Char STR [128];

Fgets (STR, 128, stdin );

Len = strlen (STR );

Printf ("Len = % d \ n", Len );

Enter "ABCD" in the standard input and press Enter.

The output result is 5 instead of 4.

So pay special attention to the use of String Length.

). Therefore, when calling the fgets function, you can only read a maximum of N-1 characters. After reading, the system automatically adds '\ 0' at the end and returns the result using STR as the function value.

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.