Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Computed values don't tie up |
| Date | 2012-04-03 14:57 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <4F7B482B.2040109@verizon.net> (permalink) |
| References | <alpine.LNX.2.00.1204031912440.4639@rebelCode.slacknet> |
On 04/03/2012 03:29 PM, rkfb wrote:
> I'm reading 'C Programming: A Modern Approach' by KN King.
>
> If I run celcius.c (as below) and enter a value of 32 (or 32.0) I am given
> the equivalent celcius temperature as -2.2. Surely it should be zero?
>
> /* Converts a Fahrenheit temperature to Celcius */
>
> #include <stdio.h>
>
> #define FREEZING_PT 32.0f
> #define SCALE_FACTOR (5.0f / 9.0f)
>
> int main(void)
> {
> float fahrenheit, celcius;
>
> printf("Enter Fahrenheit temperatue: ");
> scanf("%f", &fahrenheit);
Like most I/O functions, scanf() can fail. You should always check to
see whether or not it did fail. If so, there's no guarantee that
'fahrenheit' contains a representation of a valid floating point value;
it could contain a signaling NaN, for instance, so you should only
execute the rest of your program is scanf() succeeded.
> celcius = (fahrenheit - FREEZING_PT) * SCALE_FACTOR;
>
> printf("Celcius equivalent: ".1f", celcius);
This line has one too many double quote characters; your code as
presented here should not have compiled without diagnostics, so this
can't be your actual program. Please cut and paste the actual text of
your program; that will make it much easier to figure out what's going
wrong.
Also, if the output from your program does not end with a new-line
character, the behavior is undefined; on many systems that's not a
problem, but even where it is permitted, I think it would be better to
append a '\n' to the printf() format string.
When I correct the issues I've raised above in the obvious ways, and
type in 32, it prints out 0; so that's another reason to think that this
in not exactly your actual program.
> return 0;
> }
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Computed values don't tie up rkfb <rkfb@rebelCode.slacknet> - 2012-04-03 19:29 +0000
Re: Computed values don't tie up James Kuyper <jameskuyper@verizon.net> - 2012-04-03 14:57 -0400
Re: Computed values don't tie up Robert <rkfb1965@googlemail.com> - 2012-04-03 21:37 +0000
Re: Computed values don't tie up Robert <rkfb1965@googlemail.com> - 2012-04-03 21:39 +0000
Re: Computed values don't tie up Ike Naar <ike@iceland.freeshell.org> - 2012-04-03 20:52 +0000
Re: Computed values don't tie up James Kuyper <jameskuyper@verizon.net> - 2012-04-03 16:56 -0400
Re: Computed values don't tie up "BartC" <bc@freeuk.com> - 2012-04-03 22:11 +0100
Re: Computed values don't tie up "BartC" <bc@freeuk.com> - 2012-04-03 21:53 +0100
Re: Computed values don't tie up Robert <rkfb1965@googlemail.com> - 2012-04-03 22:56 +0000
Re: Computed values don't tie up Geoff <geoff@invalid.invalid> - 2012-04-03 20:05 -0700
Re: Computed values don't tie up Martin Ambuhl <mambuhl@earthlink.net> - 2012-04-04 00:38 -0400
Re: Computed values don't tie up "BartC" <bc@freeuk.com> - 2012-04-03 21:04 +0100
Re: Computed values don't tie up Robert <rkfb1965@googlemail.com> - 2012-04-03 21:38 +0000
Re: Computed values don't tie up Kleuske <kleuske@nowhere.net> - 2012-04-03 23:09 +0000
Re: Computed values don't tie up Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-04-04 04:42 +0100
csiph-web