TIP: Click on subject to list as thread! ANSI
echo: cis.languages
to: Bob van der Poel 76510,2203 (X)
from: David Breeding 72330,2051
date: 1995-03-27 20:32:50
subject: #20869-scanf() question

#: 20872 S3/Languages
    27-Mar-95  20:32:50
Sb: #20869-scanf() question
Fm: David Breeding 72330,2051
To: Bob van der Poel 76510,2203 (X)

 > I must admit that not only do I not advocate using scanf()...I've never
 > used it either. So, I decided to test a bit...interesting.

 > scanf("%d%d", &v1, &v2);

 > printf("V1=%d V2=%d\n", v1, v2);

 > With this program I can enter either:

 > 123 456

 > or

 > 123
 > 456

I'd never tried that..  didn't figure it would work.

 > and get proper results. Matter of fact, I can even put in extra spaces and
 > all is okay. I figured that I would have to include a \n in the control
 > string to such the CR input from the keyboard...but that really doesn't
 > work at all. Hmmm, maybe I know why I don't use this now.

No, it seems pretty tricky to me.  If you include \n in the control string, I
think you have to press  twice.

Where the problem comes in is if you:

      scanf(something).  ... you now have a CR waiting in the buffer for

the next C read;  If your next scanf is for an integer, all is well, but if
you're looking for a string, your will get ("\n") (well, an empty string if you
are using scanf.. I've been using fscanf, which does not convert the CR, but
you can limit the input.. I've used some programs where you can overrun the
string space in the program.  I've crashed some programs with an excessively
long keyboard input, and I suspect that is what it was.

All in all, I think your original advice to just not use it is the best there
is. 


                         -- David Breeding --
       CompuServe : 72330,2051              Delphi :  DBREEDING
           ***  Sent via   InfoXpress/OSK - Vr. 1.02   ***

SOURCE: compuserve via textfiles.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.