🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.gcc.help
Article #51656

Re: a real simple C question about scanf and user input

#51656
From: Marek MAKOWSKI
Date: Fri, 30 Apr 1993 13:50
70 lines
2708 bytes
Edward J. Hartnett (ejh@khonshu.colorado.edu) wrote:
: 
: Hi, Here's a section from my program:
: 
: main() {
:   int lat_zone;
:   int cell, isccp_cell, isccp_num;
:   float lat, lon;
:   float centlat[NUM_LAT_ZONES], centlon[MAX_ISCCP_NUM];
: 
:   equare();
: 
:   printf("Please enter the lat and long in degrees: ");
:   scanf("%f%f",&lat, &lon);
:   printf("%f, %f",lat,lon);
: 
: And here's what I get when I try to run this program:
: 
: > iscgrid
: Please enter the lat and long in degrees: 1.0, 1.0
: Segmentation fault
: >
: 
: 
: So obviously something is wrong with my scanf call, but what? I asked
: the guru here and he said he never uses scanf, he uses fgets to read
: from the input to a character array and then uses scanf to read that.
: Is this common proactice? Is there some better way? All advice
: welcome!
: 
: Thanks.
: --
I agree with your guru.
But if you really want to easily get into trouble you can still use scanf()
The following program compiles and runs without problems.

s1.146% cat test.c
main() {
  float lat, lon;

  printf("Please enter the lat and long in degrees: ");
  scanf("%f%f",&lat, &lon);
  printf("\n%f, %f\n",lat,lon);
}
s1.147% gcc !$
gcc test.c
s1.148% a.out
Please enter the lat and long in degrees: 1.0 2.0

1.000000, 2.000000
s1.149% echo "Now a problem will come"
Now a problem will come
s1.150% a.out
Please enter the lat and long in degrees: 1.0, 1.0

1.000000, 0.000000

Note however that there should be no "," in the input (cf 148 and 150).
I suspect that a comma might have created the problem in your program.

Scanf is fragile, especially for an incorrect input, therefore a common
(I think) practice is to read a line, tokenise it and process each
token separately.

Marek Makowski
-- 
                  Marek Makowski  | Email: marek@iiasa.ac.at
         International Institute  | Phone: +43 02236 71.5210
     for Applied System Analysis  | Fax:   +43 02236 71.313
       A-2361 Laxenburg, Austria  | Telex: 079137 iiasa a
Technical Details
Message-ID: <1993Apr30.135055.3501@iiasa.ac.at>
Path: arc1-ca.pugleaf.net!gmd.de!newsserver.jvnc.net!howland.reston.ans.net!noc.near.net!uunet!news.univie.ac.at!iiasa.ac.at!marek
References: <EJH.93Apr29155213@khonshu.colorado.edu>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 11318c,f67b5e2b3821428a
X-Google-Attributes: gid11318c,public
X-Google-ArrivalTime: 1993-04-30 07:12:13 PST
Newsgroups: gnu.gcc.help
Path: arc1-ca.pugleaf.net!gmd.de!newsserver.jvnc.net!howland.reston.ans.net!noc.near.net!uunet!news.univie.ac.at!iiasa.ac.at!marek
From: marek@iiasa.ac.at (Marek  MAKOWSKI)
Subject: Re: a real simple C question about scanf and user input
Message-ID: <1993Apr30.135055.3501@iiasa.ac.at>
Organization: IIASA, Laxenburg, Austria
X-Newsreader: TIN [version 1.1 PL6]
References: <EJH.93Apr29155213@khonshu.colorado.edu>
Date: Fri, 30 Apr 1993 13:50:55 GMT
Lines: 70