Linux C string input function gets (), fgets (), scanf () detailed

Source: Internet
Author: User

First, gets () function detailed

The Get () function is used to read a string from a standard input device (keyboard) until the carriage return ends, but the carriage return does not belong to this string .

The calling format is:

Gets (str);

Where str is a string variable (string array name or string pointer).

The gets (str) function is similar to scanf ("%s", &str) , but not identical, when you use the scanf ("%s", &str) function to enter a string, there is a problem when you enter a space will consider the input string to end.

The character after the space is processed as the next entry, but the Get () function receives the entire string of input until it returns .

#include <stdio.h> #include <stdlib.h>int main (int argc, char **argv) {char s[20], *f;printf ("Input sth\n"); Gets (s);//waits for the input string until the carriage return ends puts (s);//outputs the input string puts ("Input sth\n"), F = malloc (sizeof (f)), and gets (f);p UTS (f); return 0;}

Gets () function to understand and defect

1. Basic information

Prototype:

Char *gets (char *buffer);

The Get () function reads a line of text from the standard input and stores it in an array passed to it as a parameter. A line of text consists of a string of characters ending with a newline character (' \ n '). Before returning, the Get () function discards the newline character (' \ n ')and instead ends with ' /'.

return value:

The read succeeds, and the function returns the same pointer as buffer. The function returns a null pointer when the read-in process encounters EOF or an error occurs. When the return value is a null pointer, you can use feof () and ferror () to determine whether the function encountered EOF or an error occurred.

For example:

Char str[10];if (gets (str) = NULL)//danger! {printf ("str =%s\n", str);}

2. Function defects

The Get function does not limit the length that it reads, and the programmer should ensure that buffer has enough space, or the buffer may not hold the content read by the get, resulting in a stack overflow. If overflow, the extra characters will be written to the stack, overwriting the original contents of the stack and destroying the values of one or more unrelated variables.

Figure 1 Overflow hints

For security reasons, you can use the gets_s () function.

Second, fgets () function of the detailed

Three, scanf () function of the detailed

Linux C string input function gets (), fgets (), scanf () detailed

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.