The difference between the C language gets () and the scanf () function

Source: Internet
Author: User

Both the scanf () function and the Get () function are available for input strings, but are functionally different. If you want to enter the string "Hi Hello" from the keyboard, you should use the gets function.

The get can receive spaces, and scanf encounters a space, carriage return, and TAB key will assume that the input ends, and that it cannot receive spaces.

Char string[15]; Gets (string); /* Encountered a carriage return that the input ended */

scanf ("%s", string); /* Encountered a space thought input ended */

So when you include spaces in the string you enter, you should use the gets input.

scanf and gets the difference when getting a string

In the C language, there are at least two functions that can get a string:

1.SCANF ()

Header file: stdio.h

Syntax: scanf ("Format control string", variable address list);

When string is accepted: scanf ("%s", character array name or pointer);

2.gets ()

Header file: stdio.h

Syntax: Gets (character array name or pointer);

Both when accepting a string:

1. Different points:

SCANF cannot accept spaces, tab tabs, carriage returns, and so on, handling of the trailing carriage: keep the carriage return in the cache.

The get can accept the Space, Tab tab and carriage return, etc., to the end of the processing of carriage: receive carriage return, but replace the carriage return with.

2. The same point:

The string is automatically added after the end of the acceptance.

Example 1:

#include <stdio.h>

Main ()

{

Char ch1[10],ch2[10];

scanf ("%s", CH1);

Gets (CH2);

}

Type ASD space FG carriage return, ASD space FG Enter, then ch1= "asd\0", ch2= "ASD fg\0".

Example 2:

#include <stdio.h>

Main ()

{

Char ch1[10],ch2[10],c1,c2;

scanf ("%s", CH1);

C1=getchar ();

Gets (CH2);

C2=getchar ();

}

Type ASDFG carriage return, ASDFG Enter, then ch1= "Asdfg\0", c1= ' \ n ', ch2= "Asdfg\0", C2 needs to be entered.

SCANF: When a carriage return is encountered, the Space and tab keys are automatically added after the string, but the carriage return, the Space and tab keys remain in the input buffer.

Get: Can accept all the characters entered before the Enter key, and use ' \ n ' instead of '/'. Enter will not be left in the input buffer

Gets () used to read the string, end the input with a carriage return

SCANF () can read all types of variables

Case:

    First paragraph:
scanf ("%lf",&f); GetChar (); Gets (a); Gets (b); Second paragraph: scanf ("%lf",&f); scanf ("%s", a); scanf ("%s", b);

If the first paragraph is not getchar, the input result is incorrect even in the case of proper input.

Therefore, the scanf function encounters a whitespace character and considers the input to be complete. Get hit enter before end input.
The scanf function places the carriage return in the buffer, and the get will change the carriage return to \.

(Reprinted from: http://leoenglish.blog.163.com/blog/static/1750319852011296336486/)

The difference between the C language gets () and the scanf () function

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.