Oh, this ...! C language scanf Enter the pit of the father's place

Source: Internet
Author: User

One

Busy today, ran to a question, Ben thought 3sA poj1004, the result of 10 minutes, the most puzzling question is why the definition of a double type of variable, but with scanf input identifier with%f words, input can not be completed, that is, the input will not work, Later to find information, only to know that the original double input when the identifier is%LF, note here is L and F, not 1 and F

After the correction finally fix, minutes A to the water problem.

But then think of a is not passed,,,, do not know why. Well, ignoring this detail, there is no need to be too concerned with the water problem, the code is as follows:

1#include <cstdio>2 intMain () {3     Doublet,d;4      for(intI=0;i< A; i++){5scanf"%LF",&t);6d+=T;7     }8printf"$%.2f", d/ A);9}

Second, in addition to the above pit, scanf input also has some pit father's small details:

When using scanf input, if the input is the character%c, then scanf will be white space (space, carriage return, tab) is read into, so generally add a getchar to get the white space character.

1 scanf ("%c", &t); // in this notation, there can also be blank characters stored in t

Resolves an issue in which whitespace characters are read as Char

When doing the problem, sometimes encounter the input data to the irregular situation, such as the end of a line is not only a return, but more than a lot of whitespace , such as after the return, because the white space (Tab, carriage return, space) will be treated as char, So today we're going to solve this problem.

1, if the input does not exist specification: GetChar () or%*c

But after all, it's really troublesome to add a getchar to the back, so we can "eat" the carriage return using another scanf notation:

1 Char b; 2 int num; 3 scanf ("%c%c%*c%d", &a,&b,&num); // Watch the%*c here.

Here I show you another way, that is, %*, which means that we do not give him a variable to store after the input is read , which is equivalent to "eat" the middle character , the first two characters and the last number will be read correctly.

If we use this point at the end of the scanf format string, we can "eat" the carriage return, lest I forget, I will write it down:

This use of asterisks is called "Assignment forbidden character". In the return value of scanf, this amount of%* is not calculated, that is, the value returned by scanf is the number of variables that were successfully deposited.

1 // use of assignment forbidden characters 2 Char a,b,c; 3 scanf ("%c%c%c%*c", &a,&b,&c); // The carriage return will be eaten .

However, if you enter the string%s, scanf ignores the white space character.

Also,%s can actually be stored in characters, but the disadvantage of%s is that if you store the value in a character, only the first character is stored, and the remaining characters are discarded, causing the loss of the character

1 scanf ("%s", &t); // may cause character loss

2. If the data is not standardized, it is not guaranteed to conform to the format, the more secure method is as follows:

scanf skips all whitespace characters before%c, and, in the same vein, if it is not a space but \ t or \ n, all tab and all carriage returns are skipped.

1 scanf ("  %c", &t); // note here there are spaces before%c!!! 

This time there is a problem because this WA for two days, the Pit dead me ... 5555555555

Oh, this ...! C language scanf Enter the pit of the father's place

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.