Article View: comp.sys.ibm.pc
Article #42662Re: scanf problem in TC v2.01.
From: D'Arcy J.M. Cain
Date: Mon, 19 Feb 1990 15:45
Date: Mon, 19 Feb 1990 15:45
52 lines
2082 bytes
2082 bytes
In article <204@sdscal.UUCP> keith@sdscal.UUCP (Keith Jones) writes:
>Well, I didn't quite believe it, but I made this test file and sure enough,
>the printf doesn't print out anything that even looks like it approximates
>PI.
>
>----- to cut ----- or not to cut -----
>main()
>{
> float f;
> char *string = "3.1415926";
>
> f = atof( string );
> printf( "pi = %f\n", f );
>}
>----- be it here ----- or there -----
>
>The output to this program was:
>
>D:\ -> test
>
>-8209.00000
>
>and I compiled it with:
>
>D:\ -> tcc test
>
>I'm not particularly worried about this bug, but I figured I might as well
>post the program.
>
There are a number of things you can do to fix this problem. The first is
to pull out that reference manual and look up atof's prototype:
double atof(char *str);
Add the above line to your code and it should work. You may also want to
make f a double and change the printf statement to:
printf( "pi = %lf\n", f );
this will give you a little more precision.
The moral of the story is *ALWAYS* compile with as many warnings turned
on as your compiler will allow and prototype everything. I do this on
Turbo C, Microsoft C, GNU C and any compiler that allows it and I don't
consider a program correct until it compiles without a whisper. That
philosophy would have caught the above "bug."
--
D'Arcy J.M. Cain (darcy@druid) | Thank goodness we don't get all
D'Arcy Cain Consulting | the government we pay for.
West Hill, Ontario, Canada |
(416) 281-6094 |
Technical Details
Message-ID:
<1990Feb19.154506.8830@druid.uucp>Path:
arc1-ca.pugleaf.net!utzoo!attcan!telly!druid!darcyReferences:
<7192.25d1e107@dit.ie> <25D4D935.3073@maccs.dcss.mcmaster.ca> <25D8649A.24584@maccs.dcss.mcmaster.ca> <204@sdscal.UUCP>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!telly!druid!darcy From: darcy@druid.uucp (D'Arcy J.M. Cain) Newsgroups: comp.sys.ibm.pc Subject: Re: scanf problem in TC v2.01. Message-ID: <1990Feb19.154506.8830@druid.uucp> Date: 19 Feb 90 15:45:06 GMT References: <7192.25d1e107@dit.ie> <25D4D935.3073@maccs.dcss.mcmaster.ca> <25D8649A.24584@maccs.dcss.mcmaster.ca> <204@sdscal.UUCP> Reply-To: darcy@druid.UUCP (D'Arcy J.M. Cain) Organization: D'Arcy Cain Consulting, West Hill, Ontario Lines: 52