Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Proper use of scanf

Reply
Thread Tools

Proper use of scanf

 
 
Lefty Bigfoot
Guest
Posts: n/a
 
      06-18-2005

Hello,

I am aware that a lot of people are wary of using scanf,
because doing it improperly can be dangerous. I have
tried to find a good tutorial on all the ins and outs
of scanf() but been unsuccessful.

Is there a well-respected (by the c.l.c crowd) book
or tutorial that really covers scanf in detail?
 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      06-19-2005
Lefty Bigfoot wrote:
>
> Hello,
>
> I am aware that a lot of people are wary of using scanf,
> because doing it improperly can be dangerous. I have
> tried to find a good tutorial on all the ins and outs
> of scanf() but been unsuccessful.
>
> Is there a well-respected (by the c.l.c crowd) book
> or tutorial that really covers scanf in detail?


I don't know of one.
Here's two examples of one way to use scanf safely.
If you enter more than LENGTH characters,
the extra ones will be automatically discarded.
If you have questions, I can try to explain.
The whole topic of scanf is somewhat lengthy.

/* BEGIN anystring.c */

#include <stdio.h>

#define LENGTH 20
#define str(x) #x
#define xstr(x) str(x)

int main(void)
{
char string[LENGTH + 1];
int rc;

printf("Enter any string or enter an empty string to quit: ");
fflush(stdout);
rc = scanf("%" xstr(LENGTH) "[^\n]%*[^\n]", string);
if (!feof(stdin)) {
getchar();
}
while (rc == 1) {
printf("Your string was %s\n", string);
printf("scanf() returned %d\n", rc);
printf("Enter any string or enter an empty string to quit: ");
fflush(stdout);
rc = scanf("%" xstr(LENGTH) "[^\n]%*[^\n]", string);
if (!feof(stdin)) {
getchar();
}
}
printf("scanf() returned %d\n", rc);
return 0;
}

/* END anystring.c */


/* BEGIN grade.c */

#include <stdlib.h>
#include <stdio.h>

#define LENGTH 3
#define str(x) # x
#define xstr(x) str(x)

int main(void)
{
int rc;
char array[LENGTH + 1];
long number;
const char letter[4] = "DCBA";

fputs("Enter the Numeric grade: ", stdout);
fflush(stdout);
rc = scanf("%" xstr(LENGTH) "[^\n]%*[^\n]", array);
if (!feof(stdin)) {
getchar();
}
while (rc == 1) {
number = strtol(array, NULL, 10);
if (number > 60) {
if (number > 99) {
number = 99;
}
array[0] = letter[(number - 60) / 10];
switch (number % 10) {
case 0:
case 1:
array[1] = '-';
array[2] = '\0';
break;
case 8:
case 9:
array[1] = '+';
array[2] = '\0';
break;
default:
array[1] = '\0';
break;
}
} else {
array[0] = 'F';
array[1] = '\0';
}
printf("The Letter grade is: %s\n", array);
fputs("Enter the Numeric grade: ", stdout);
fflush(stdout);
rc = scanf("%" xstr(LENGTH) "[^\n]%*[^\n]", array);
if (!feof(stdin)) {
getchar();
}
}
return 0;
}

/* END grade.c */


--
pete
 
Reply With Quote
 
 
 
 
Jean-Claude Arbaut
Guest
Posts: n/a
 
      06-19-2005

Le 19/06/2005 00:54, dans http://www.velocityreviews.com/forums/(E-Mail Removed),
«*Lefty Bigfoot*» <(E-Mail Removed)> a écrit*:

>
> Hello,
>
> I am aware that a lot of people are wary of using scanf,
> because doing it improperly can be dangerous. I have
> tried to find a good tutorial on all the ins and outs
> of scanf() but been unsuccessful.
>
> Is there a well-respected (by the c.l.c crowd) book
> or tutorial that really covers scanf in detail?


Yes, the ISO 9899-1999 Standard, section 7.19.6.4.
I can swear it's well respected here

There is also the POSIX standard, at
http://www.opengroup.org/onlinepubs/000095399/
certainly well respected too.

If you want a tutorial, maybe the K&R C book will help you.

If you have UNIX or Linux, try "man scanf"

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      06-19-2005
Lefty Bigfoot wrote:
>
> I am aware that a lot of people are wary of using scanf,
> because doing it improperly can be dangerous. I have
> tried to find a good tutorial on all the ins and outs
> of scanf() but been unsuccessful.
>
> Is there a well-respected (by the c.l.c crowd) book
> or tutorial that really covers scanf in detail?


Take a look at the links in my sig. Especially the one marked
C-library, and then the C99 one.

--
Some useful references about C:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://www.eskimo.com/~scs/C-faq/top.html>
<http://benpfaff.org/writings/clc/off-topic.html>
<http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/> (C99)
<http://www.dinkumware.com/refxc.html> (C-library}
<http://gcc.gnu.org/onlinedocs/> (GNU docs)

 
Reply With Quote
 
Vivek
Guest
Posts: n/a
 
      06-19-2005
There seems to be some problem with the way I am using scanf() in my
code. Could please help me out?

/*Sample Program*/

#include<stdio.h>

int main()
{
double D;

scanf("%g", &D);
printf("%g", D);

return 0;
}

/*During Execution*/
1232 /*Input*/

5.68642e-315/*Output*/

/*End*/

If it helps, I am using the gcc compiler(ver 3.4.3) that I got with
DJGPP and I am using it on Win98.

 
Reply With Quote
 
Vivek
Guest
Posts: n/a
 
      06-19-2005
Hello,

I just found a post from a person having the same problem. How cool is
that! The one mistake I found out is that '%g' in the scanf should be
'%lg'. Anyother nit picking will be welcomed.

 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      06-19-2005
Vivek wrote:
> There seems to be some problem with the way I am using scanf() in my
> code. Could please help me out?
>
> /*Sample Program*/
> #include<stdio.h>
> int main()
> {
> double D;
> scanf("%g", &D);
> printf("%g", D);
> return 0;
> }
>
> /*During Execution*/
> 1232 /*Input*/
> 5.68642e-315/*Output*/



#include<stdio.h>

int main(void)
{
double D;
char input[] = "1234";

printf("Using the OP's incorrect specifier for\n"
"reading the string \"%s\" as a double:", input);
sscanf(input, "%g", &D);
printf("%g\n", D);

printf("Using the correct specifier for\n"
"reading the string \"%s\" as a double:", input);
sscanf(input, "%lg", &D);
printf("%g\n", D);

return 0;
}

[output]
Using the OP's incorrect specifier for
reading the string "1234" as a double:2.88675e-13
Using the correct specifier for
reading the string "1234" as a double:1234

> If it helps, I am using the gcc compiler(ver 3.4.3) that I got with
> DJGPP and I am using it on Win98.


If you had turned on the warnings during compilation, gcc would have
told you the specifier was wrong. Not all compilers are so obliging.
Keep those warning levels up, and don't ignore the diagnostics.



 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      06-19-2005
Vivek wrote:
> There seems to be some problem with the way I am using scanf() in my
> code. Could please help me out?
> [...]
> double D;
> scanf("%g", &D);
> [...]
>
> If it helps, I am using the gcc compiler(ver 3.4.3) that I got with
> DJGPP and I am using it on Win98.


The compiler you're using happens to be able to
catch this error for you, if you ask it nicely. Use
the command-line flags "-Wall -W"; for code that's
intended to be portable use "-Wall -W -ansi -pedantic".
(There are alternatives to "-ansi"; see the info pages.)

--
Eric Sosman
(E-Mail Removed)lid
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      06-19-2005
Eric Sosman wrote:
> Vivek wrote:
>
>> There seems to be some problem with the way I am using scanf()
>> in my code. Could please help me out?
>> [...]
>> double D;
>> scanf("%g", &D);
>> [...]
>>
>> If it helps, I am using the gcc compiler(ver 3.4.3) that I got
>> with DJGPP and I am using it on Win98.

>
> The compiler you're using happens to be able to
> catch this error for you, if you ask it nicely. Use
> the command-line flags "-Wall -W"; for code that's
> intended to be portable use "-Wall -W -ansi -pedantic".
> (There are alternatives to "-ansi"; see the info pages.)


This is one more of a long list of anomalies in scanf, and adds to
the list of reasons not to use it. Unfortunately the C library
lacks good routines to input from streams (rather than buffers). I
have written and published some suitable routines in the past.

To the OP - look up the modifier l (small ell) in the scanf
documentation. Read it with great care. Then don't use it, get
complete lines and work on those.

--
Some informative links:
news:news.announce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html


 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      06-20-2005
On Sun, 19 Jun 2005 02:08:52 +0200, Jean-Claude Arbaut
<(E-Mail Removed)> wrote in comp.lang.c:

>
> Le 19/06/2005 00:54, dans (E-Mail Removed),
> «*Lefty Bigfoot*» <(E-Mail Removed)> a écrit*:
>
> >
> > Hello,
> >
> > I am aware that a lot of people are wary of using scanf,
> > because doing it improperly can be dangerous. I have
> > tried to find a good tutorial on all the ins and outs
> > of scanf() but been unsuccessful.
> >
> > Is there a well-respected (by the c.l.c crowd) book
> > or tutorial that really covers scanf in detail?

>
> Yes, the ISO 9899-1999 Standard, section 7.19.6.4.
> I can swear it's well respected here


That's actually not a reference to how to use the function safely, nor
even a recommendation that it should be used.

See 7.19.7.7, which describes the gets() function, and includes not so
much as a hint of a warning about buffer overflows.

> There is also the POSIX standard, at
> http://www.opengroup.org/onlinepubs/000095399/
> certainly well respected too.
>
> If you want a tutorial, maybe the K&R C book will help you.
>
> If you have UNIX or Linux, try "man scanf"


The general consensus here, which I agree with, is that the *scanf()
functions are best avoided by all except extreme experts on their use.
If non-experts avoid them, the will never become extreme experts,
making the point moot.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
difference between scanf("%i") and scanf("%d") ??? perhaps bug inVS2005? =?ISO-8859-1?Q?Martin_J=F8rgensen?= C Programming 18 05-02-2006 10:53 AM
scanf (yes/no) - doesn't work + deprecation errors scanf, fopen etc. =?ISO-8859-1?Q?Martin_J=F8rgensen?= C Programming 185 04-03-2006 02:49 PM
how to use scanf()? =?utf-8?B?55ee5a2Q?= C Programming 1 05-24-2005 07:10 AM
why to use '&' in scanf( ) but not in printf( ) sushant C Programming 4 01-10-2005 04:22 PM
Proper way to use an imported constant under 'use strict'? H. Wade Minter Perl Misc 8 04-25-2004 12:58 AM



Advertisments