[aclug-L] Re: C problem redux....
[Top] [All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Larry Bottorff wrote:
>
> Here's some code that's confusing me:
>
> #include <stdlib.h>
>
> int main()
> {
> char number[] = {"5.333009"};
> double dval;
> dval = atof("5.333009");
> printf("number: %s\n", number);
> printf("number: %lf\n", dval);
>
>
> }
>
> Here's the output:
>
> number: 5.333009
> number: 349440.000000
Change %lf to %f. %lf assumes that the argument is a long double, not a
double. Therefore, it factors extra bits of nonsense into the number.
The way printf() works is that it looks at the format string for format
codes to tell it what the argument types are. However, the compiler
doesn't know nor care what the further arguments are, and doesn't look
at the format string to check them. It is up to you the programmer to
make sure that the argument types match.
> Where on earth is it getting 349440.000000 ? I get the same results when
> I try atof(number). Sunday is an odd day, right?
>
> Larry Bottorff
>
> -- This is the discussion@xxxxxxxxx list. To unsubscribe,
> visit http://tmp2.complete.org/cgi-bin/listargate-aclug.cgi
--
/*
* Tom Hull * thull@xxxxxxxxxxx * http://www.ocston.org/~thull/
*/
-- This is the discussion@xxxxxxxxx list. To unsubscribe,
visit http://tmp2.complete.org/cgi-bin/listargate-aclug.cgi
[aclug-L] Re: C problem redux....,
Tom Hull <=
|
|