C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-17-2009, 10:08 AM   #1
Complete Beginner
 
Join Date: Feb 2009
Posts: 312
scanf() consideres useless

Hello everyone,

there seem to be a lot of questions on this board regarding the correct use of scanf(), most of which advert to a weak understanding of pointers.

Personally, I haven't been using scanf() since I passed the initial 20 pages of my first book on C about ten years ago. Since then, I've always been using a (possibly dynamic) array to read data one character at a time and then check the input using isdigit(), atoi(), strtod(), whatever.

I think the underlying problem with scanf() is that it frequently appears early in the literature, way before the infamous chapter about pointers. At this point, the reader isn't familiar with the concept of a variable as the 4-tupel consisting of name, value, address and size and thus must regard the &-operator as some magic that is required to get scanf() to work as intended.


What's your opinion about the usefulness of scanf()?
Are you actually using it on a frequent basis?

Greets,
Philip
__________________
All things begin as source code.
Source code begins with an empty file.
-- Tao Te Chip
Snafuist is offline   Reply With Quote
Old 02-17-2009, 10:12 AM   #2
Registered User
 
Join Date: Feb 2009
Posts: 278
I use it very rarely. The method I use depends on what I'm coding, but I agree with what you say. The only thing I use it for regularly is a "press enter when ready" situation.
Bladactania is offline   Reply With Quote
Old 02-17-2009, 10:14 AM   #3
critical genius
 
MK27's Avatar
 
Join Date: Jul 2008
Location: SE Queens
Posts: 5,226
I would almost echo your thoughts except my timeframe is like ten months and not ten years. But I also got taken in by GNU getline() which works quite well. After I while I started using my own little getline() like functions which work byte by byte, as you say, and this DEFINATELY is the most optimized and tweakable way to deal with input.

But there's nothing wrong with scanf or sscanf or fscanf. Specific criticisms of specific books, I have lots of those too I suppose I didn't bother much with the "scan" functions 'til I did get a real grip on pointers.
__________________

"A man can't just sit around." -- Larry Walters
MK27 is offline   Reply With Quote
Old 02-17-2009, 10:19 AM   #4
Registered User
 
Join Date: Sep 2006
Posts: 3,156
I use scanf() to read in "clean" data that the program has produced in another function. For helping students, it seems to be necessary as well. I believe it's the fastest method to read data if you know it's absolutely formatted properly.

Otherwise, I stick with fgets(), into a buffer, and test it from there.
Adak is offline   Reply With Quote
Old 02-17-2009, 11:02 AM   #5
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,858
I'm with Adak -- if I'm reading formatted data (often programmatically generated), then scanf is ideal. If I'm doing something quick in class on the big screen, I'll often use it there rather than find/create/import a whole input-verification suite. (Actually, usually I'm in C++ so it's >>, but same moral difference.) But for "real" user input, line-by-line is the way to go.
tabstop is offline   Reply With Quote
Old 02-17-2009, 11:09 AM   #6
Complete Beginner
 
Join Date: Feb 2009
Posts: 312
I should have mentioned that I don't believe in correctly formatted data except when I created it myself 5 minutes ago. In this case I refer to Perl, sh, bintools or whatever quick solution is available. A given format specification might have been correct on a particular Tuesday of last year, but in order to build stable code, I don't trust anyone (including me) stating that his output will always look like this-and-that.
__________________
All things begin as source code.
Source code begins with an empty file.
-- Tao Te Chip
Snafuist is offline   Reply With Quote
Old 02-17-2009, 12:17 PM   #7
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
It is all about horses for courses. If you have file-data that is (supposedly) machine generated, then reading it with fscanf() is perfectly file - just check that the result is what you expect from the return value.

For simple programs that do not need much input-validation, it is also fine.

But for proper user-input, better methods are indeed necessari. strtol() is better than atol or atoi, in my opinion, as it allows you to detect both overflow and "extra garbage at the end" - and you can control the base if you wish too.

It serves it purpose as a simple method for input, but as stated, it is very easy to get it wrong in various ways, including user input that "blocks" any further input.

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Old 02-17-2009, 12:24 PM   #8
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,768
The majority of problems I see on this board related to scanf() are related to leaving junk in the buffer after a failed conversion, not pointer issues.

While one can learn how to properly use pointers, it is not possible by simply wishing, to cause scanf() to become a properly useful function.
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline   Reply With Quote
Old 02-17-2009, 02:35 PM   #9
Registered User
 
Join Date: Feb 2009
Posts: 138
Quote:
Originally Posted by Snafuist
I think the underlying problem with scanf() is that it frequently appears early in the literature, way before the infamous chapter about pointers. At this point, the reader isn't familiar with the concept of a variable as the 4-tupel consisting of name, value, address and size and thus must regard the &-operator as some magic that is required to get scanf() to work as intended.
there's nothing wrong with magic. i don't think pointers need to be introduced to explain scanf. just say that every variable except an array gets prepended with '&' and the reason why will be made clear later. you don't have to understand a rule to memorize and follow it.

i use scanf all the time. there's no reason to use fgets and atoi when scanf is shorter, simpler and does what i want.
Meldreth is offline   Reply With Quote
Old 02-17-2009, 03:07 PM   #10
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,768
Quote:
Originally Posted by Meldreth View Post
i use scanf all the time. there's no reason to use fgets and atoi when scanf is shorter, simpler and does what i want.
So how do you recover cleanly from malformatted inputs? Or do you just neglect input validation?
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline   Reply With Quote
Old 02-18-2009, 07:18 AM   #11
Registered User
 
Join Date: Feb 2009
Posts: 138
the same way i recover with other methods. if the input is malformed, ask for it again. it's not rocket science.
Meldreth is offline   Reply With Quote
Old 02-18-2009, 07:56 AM   #12
Mysterious C++ User
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 14,785
The problems with scanf is that it requires a lot knowledge to use securely and without hitches.
Input validation must be done, buffer cleaning must be done, etc. So I believe it's not as much pointer issues, but generally a lack of know-how to properly use it. But you could argue that most who have problems with it simply ignored most of what they learned simply because it can be difficult to know everything. C is a difficult language, especially for beginners.
So I would partly lay blame on the books (because there are lot of bad ones out there) and partly on newbies who do not fully understand C. You could also lay blame on poor teachers. We've seen that happen, too.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 02-18-2009, 07:58 AM   #13
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Further, scanf() doesn't play well with other input methods, e.g. fgets() or getchar(), since it leaves the terminating character(s) in the input buffer. This in itself is a very good reason for using fgets() for everything, if fgets() is ever needed in the code. If you still like to use scanf(), then use fgets() + sscanf(),

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Old 02-18-2009, 08:26 AM   #14
Complete Beginner
 
Join Date: Feb 2009
Posts: 312
Quote:
you don't have to understand a rule to memorize and follow it.
I do. But as far as I can tell, this is the only easy way to make a beginner able to read input data. The standard library lacks simple input functions like e.g. getdouble(), getint() and so on, which is what my rant on scanf() is all about: I don't like scanf() because it's kind of an all-in-one wonder function, but what really disturbs me is the fact that there is no simple standardized alternative. If you're already familiar with the complexity of scanf(), input buffers and the like, then it seems natural to say scanf("%d", &n) to read an int and having to check the return value, but for a beginner, this has to look like voodoo. Now, C's goal is probably not to be friendly to beginners, but I can also see issues here concerning efficiency and design. If you were to write a function to read input, would you choose char* and "..." as its arguments?

Quote:
it is not possible by simply wishing, to cause scanf() to become a properly useful function.
brewbuck, I don't get your point, but this is maybe due to my poor English skills. To me, it seems that you regard scanf() to be not properly useful, is that right?

Greets,
Philip
__________________
All things begin as source code.
Source code begins with an empty file.
-- Tao Te Chip
Snafuist is offline   Reply With Quote
Old 02-18-2009, 08:32 AM   #15
Registered User
 
Join Date: Feb 2009
Posts: 278
Quote:
Originally Posted by Snafuist View Post
I don't get your point, but this is maybe due to my poor English skills.

Really? You're probably one of the better English speaking people here!
Bladactania is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with a basic scanf procedure. killpoppop C Programming 9 11-03-2008 04:39 PM
Odd data entry error, reading an unsigned integer with scanf Xipher C Programming 2 02-20-2005 01:48 PM
scanf issue fkheng C Programming 6 06-20-2003 07:28 AM
Scanf and integer... penny C Programming 3 04-24-2003 06:36 AM
scanf - data is "put back" - screws up next scanf voltson C Programming 10 10-14-2002 04:34 AM


All times are GMT -6. The time now is 06:43 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22