J
James Kuyper
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.