32

You've probably seen it. The thousands of different questions where the solution is changing scanf("%c to scanf(" %c, that is, put a space before %c. At first it was funny, but I do get tired of it because it is so extremely common.

Should anything be done about it? If so, do you have any suggestions? Maybe you could get a popup suggesting that solution in combination with a link to the documentation if your code contains a scanf.

Of course there will always be questions that are asked fairly common here at SO. That's just the way it is, but this is so common that I think it could be reasonable with some kind of action. What do you think?

Example: Why is this loop unending? This is a C program I'm forced to create my own file as you'll see

Also, since these people don't know what the problem is, you cannot really blame them for not searching for similar questions. The questions are unique. It's the problem and solution that's the same.

11
  • 19
    Isn't there a good (canonical) duplicate explaining (all) the scanf() formats in detail you can propose there?
    – user0042
    Commented Oct 14, 2017 at 18:34
  • 2
    That one is also missing an address operator, another 'C programming 101' issue:( Commented Oct 14, 2017 at 18:42
  • 9
    I've essentially given up with the C tag. If considering Q&A that will be useful to future users/visitors, there is next-to-nothing there worth bothering with. You could spend all day just linking dupes, and thereby spending much more time on the questions than the OP's do before posting:( Commented Oct 14, 2017 at 18:49
  • 5
    It has a dedicate tag, [scanf] has 4484 questions. That probably still doesn't help the newbies to fix their bug, but it does help you. Add the tag to your Ignored Tags section in your profile so you don't have to look at them again. And when you run into one anyway because it wasn't tagged correctly then just add the tag. Commented Oct 14, 2017 at 19:32
  • 1
    Soon we'll not have to answer anymore. Solution will popup.
    – TGrif
    Commented Oct 14, 2017 at 20:39
  • 18
    Mark it as a duplicate of scanf() leaves the newline char in buffer. Or state that it is a duplicate of that. It's the question I use as 'canonical' for that problem. There are a collection of other questions that are canonical duplicates for other aspects of scanf(). If you've only been around 14 months or so and are worn out, you need to develop a thicker skin. It doesn't matter how many people you teach this week about the problems with scanf() (and there are many problems with it); there'll be new people to teach next week. Commented Oct 15, 2017 at 0:55
  • 12
    Relevant. Also, @Jonathan, please consider moving that comment into the answer box so that I can give it a proper upvote. :-) Linking to the canonical question about scanf is the correct answer to this question, in my opinion.
    – Cody Gray Mod
    Commented Oct 15, 2017 at 6:48
  • @JonathanLeffler one of the answers suggests fflush(stdin). this should be deleted IMHO... Commented Oct 17, 2017 at 5:19
  • @Jean-FrançoisFabre: Thanks for pointing that out. There are 2 delete votes in place. Also a comment cross-referencing the canonical question on Using fflush(stdin). I believe I close-voted that answer before, but it lapsed as no-one else was paying attention. Commented Oct 17, 2017 at 5:24
  • @JonathanLeffler I would not say that I'm worn out. Not by far. It's just that I thought this could be one of the aspects where SO could improve.
    – klutt
    Commented Oct 17, 2017 at 10:49
  • If you are thinking "there must be better ways to manage duplicate finding", then you are probably correct, and there have been other questions and answers about that on MSO before. For example, I've made a suggestion in my answer to Would the SO community bbenefit form an AI mechanism to help moderators identify duplicates? There are links to other questions from there. Commented Oct 17, 2017 at 14:53

2 Answers 2

41

As originally observed in a comment, for this particular problem, mark it as a duplicate of scanf() leaves a newline char in buffer. However, it is hard for a novice C programmer to find this solution to their problem — if they realized that the newline was left in the buffer, they probably wouldn't be asking the question in the first place.

There is a collection of other questions that are appropriate targets for duplicates of other aspects of scanf(). What follows is by no means a definitive list — for example, it doesn't include an entry for "you forgot to put & in front of simple variables", or "you put & in front of a (character) array name", or "you must check the return value from scanf()".

Unfortunately, scanf() and family are some of the most complex functions out there, and among the hardest to use effectively. They do work for the simple cases if you know what you're doing and nothing goes wrong; they become problematic if you don't know what you're doing or if things go wrong.

It would be nice if there were some other input functions that were more difficult to misuse, but there aren't standard functions for that task. The CS50 course (see questions tagged , the CS50 Stack Exchange site, the CS50 home page at Harvard, and the CS50 GitHub repository) provides a series of functions for single value inputs that are effective and hard to misuse (though there are aspects of their design that can be debated).

One other thing to remember is that the same issues are asked about over and over again because, despite everything, it is not easy to search for certain problems, especially when you don't really understand what's going wrong. On Stack Overflow, there is a continual influx of new programmers. Those who learned last week about the issues with scanf() will be replaced by a new crew this week. (And C has a particularly cyclical pattern to novice questions, peaking in the autumn and early winter as the newcomers to university courses are starting their exercises.) You simply have to accept that there will be lots of similar questions and get good at diagnosing them. Look at the tag wiki for the tag too. It's big but has quite a lot of useful information in it.

4
  • One problem I see with marking it as a duplicate it that these questions commonly have more than just this problem. After all, the typical scanf question is from a beginner who's asking "Why doesn't my code work?" What's your opinion on that?
    – klutt
    Commented Oct 16, 2017 at 22:16
  • 1
    @klutt: It depends on mood, stress-level, time available, etc. If there's a primary problem that will resolve most of the issues, I'll duplicate to that, often with a comment about other bits and pieces. If there are several standard issues, I'll edit the duplicate list to include the various alternatives. If there are so many problems that neither of these makes sense, then I'll consider an answer which cites the possible duplicates and deals with whatever the other issues are. I'll try to duplicate rather than close for other reasons, too. Or I'll annotate to explain some of the issues. Commented Oct 16, 2017 at 22:22
  • Sounds resonable
    – klutt
    Commented Oct 17, 2017 at 10:45
  • I added an answer pointing at one of the canonical duplicates used for these cases. The answer is community wiki so we can edit it. It might be wise to have one single, canonical duplicate for all *scanf-related questions. If the links pointed out in the answer by @JonathanLeffler provide valuable info which is missing in the community wiki answer, then perhaps we can integrate them there instead.
    – Lundin
    Commented Nov 6, 2017 at 15:24
0

The vast majority of these questions are regarding a trailing line feed character in stdin which was not cleared. And there's also questions about using various bad methods such as gets() or fflush(stdin). This is why a community wiki was written to address all these FAQs.

How to read / parse input in C? The FAQ

It is fine to use this as a duplicate for all such FAQs. The original author has actively been maintaining and expanding the answer, which is great, but anyone who feel that they have something to add could do so as well.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .