C scanf Functions

Source: Internet
Author: User

CLanguageScanfFunction Adventure

Prepared by: ocean:

Blog: http://oceanspace.tk

 

When I read The scanf function section in The C Programming Language, I randomly typed a few lines of code. I thought it was very simple, and I was a little confused, I didn't expect this question to happen. It involves a lot of things. Haha! I would like to share my experience with you in the hope that it will help you.

I. code example

The code I typed at that time:

#include<stdio.h>

int main()

{

int a;

int b;

char mon[20];

int count;



count = scanf("%d,%s,%d", &a,mon,&b);

printf("%d,%s,%d\n",a,mon,b);

printf("%d\n",count);



return 0;

}

Running result:

Ocean @ ocean-desktop :~ /Desktop $./re

12, fefe, 45/* this is my input */

12, fefe, 45, 10359588

2

The result looks like what we want, but there is a strange number in the end. But let's take a closer look at the count value and wonder, how is it 2 or 3? Why does scanf read-only two values? What is going on? Debug it with gdb to see what a and mon are.

II,GDBDebugging status

(Gdb) p

$1 = 12

(Gdb) p mon

$2 = "fe, 45 \ 000 \ 377 \ 277 \ 245 \ 324 \ 025 \ 000 \ 060 \ 340 \ 021 \ 000K \ 205 \ 004 \ B"

Do you understand? Originally fe and 45 were stored in mon as a whole, and B did not read the value at all, displays a mess of values in the original memory (if you don't believe it, you can assign a value to B at the beginning of the program. The final result must be the original value, because B is not read into the new value at all), scanf really reads only two values, so count shows 2. Why? Let's take a look at the scanf function information.

III,ScanfHow functions work

Scanf () reads values from the input stream buffer rather than from the keyboard (terminal) buffer. Sending data to the input stream buffer ends with a carriage return (\ n). This \ n will be read into the input stream buffer together. After scanf () starts reading the input, it will stop reading at the first blank space (blank), tab (tab), or newline.

When the format control string contains common characters (non-format characters), these characters are used as the separator of the input data and are automatically removed when the scanf function reads data.

If the % s specifier is used in the scanf () format control string, all characters other than the white space characters are acceptable, so scanf () the white space character is skipped until the first non-white space character is encountered, and all non-white space characters before the white space character is saved again. This means that % s enables scanf () to read a word, that is, a string that does not contain spaces.

Well, let's analyze how the above results appear.

Iv. Cause Analysis

First, scanf () skips the white space (not here, because the first character is 1) until a non-white space character 1 is encountered, and then continues to read 2, when reading the non-numeric symbol "comma", scanf knows that the integer has been read, and assigns 12 to a. At this time, the first character starting with the input stream buffer is a comma; scanf continues reading, read that the comma matches the comma of the format control string, pass; Continue reading from f, always read the next blank character -- press enter at the end (scanf automatically removes this carriage return character, the string is not sent to the string.) after reading the string, the first character starting with the input stream buffer is the carriage return character. Continue reading. The carriage return character is not matched with the comma in the format control string, failed to read. To sum up, scanf does read only an integer and a string, and the return value is 2.

Is there any way to use a comma as the delimiter? The following two methods are provided:

5. Solution

Method1:

Scanf ("% d, % [^,], % d", & a, mon, & B );

Printf ("% d, % s, % d \ n", a, mon, B );

Knowledge: scanf is a rare but useful conversion character: [...] and [^...]

% [...] If the entered character belongs to a character in the character string in square brackets, the character is extracted. If not found, the extraction ends. % [^...] If the input character is found to belong to a character in the string in square brackets, the extraction ends. If it does not belong to the character, the extraction ends. The two methods automatically add a string terminator to the end of the extracted character. For example:

#include<stdio.h>

main()

{

char strings[100];

scanf("%[1234567890]",strings);

printf("%s",strings);

return 0;

}

Run. Input 1234werew and the result is 1234.

In this way, after reading fefe, a comma is used to end reading the string. When reading continues, the comma in the input stream buffer exactly matches the comma in the format control string!

Method2(Incomplete):

Scanf ("% d, % s, % d", & a, mon, & B);/* Note that % s is followed by a space */

Printf ("% d, % s, % d \ n", a, mon, B );

And add a space when entering

12, fefe, 45/* Add a blank space between fefe and comma */

Knowledge: When the scanf () format control string is blank, it indicates that any blank space is skipped when the number is obtained.

Scanf reads the space after Fefe and ends reading the string. At this time, the first character in the input stream buffer is space. Continue reading because the format control string contains a space, therefore, any blank space will be skipped during reading (do not believe you can try again after Fefe, skip all the spaces, or even skip all the blank spaces), and read that the comma matches successfully.

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.