tags:

views:

146

answers:

6
#include <stdio.h>
#include <stdlib.h>

int main (void)
{
double f;

printf ("What is the temperature in Fahrenheit?\n");
scanf ("%d", &f);

double x = (f-32)*(5/9);
printf ("%d degrees Fahrenheit \n",f); 
printf ("%d degrees Celsius",x); 
system("PAUSE");  

return 0;
}

f seems to be printing address of variable f instead of the value, its probably a syntax mistake.

+7  A: 

%d should be %f:

printf ("%f degrees Farenheit \n",f); 
printf ("%f degress Celsius",x); 

EDIT: As Jonathan noted, you need to change:

double x = (f-32)*(5/9);

to

double x = (f-32)*(5.0/9);

In C, integer division (a division where both dividend and divisor are integer types) truncates, so 5/9 is always 0.

Matthew Flaschen
Use "%f" for both float and double; a float argument to a variadicfunction like printf is automatically promoted to double.
jschmier
+1. %f should do. http://www.thinkage.ca/english/gcos/expl/c/lib/printf.html
codaddict
That's (a major) part of the problem - it is not the whole problem. The other problem is the 5/9 == 0 issue.
Jonathan Leffler
A: 

adding to what others have said, %d stands for "decimal" (as opposed to %x being "hexadecimal")

for floating point numbers, or in your case double precision floating point numbers, use %f

cobbal
+3  A: 

%d means "decimal integer". You are using doubles, not ints. So use %lf in the scanf() and %f in the printf().

Dave Hinton
+2  A: 

Try this...

#include <stdio.h>
//#include <stdlib.h>

int main (void)
{
float c;
float f;

printf ("What is the temperature in Fahrenheit?\n");
scanf ("%f", &f);

c = (f-32)*(5.0/9.0);
printf ("%f degrees Fahrenheit \n",f);
printf ("%f degrees Celsius \n",c);
system("PAUSE");

return 0;
}
Better if you actually include <stdlib.h> - it declares 'system()'.
Jonathan Leffler
A: 

9 times out of 10, when you use printf to display debug data and find it displaying bizzare results, you have a bug in your printf call.

This is why I now try to avoid printf. The dynamic format bit is neat, but the extreme type-unsafety of the call makes it too often a source of bugs rather than enlightenement.

T.E.D.
Get yourself a compiler that tells you when you make mistakes - and learn the rules so you don't make mistakes.
Jonathan Leffler
+2  A: 

Your first problem is double x = (f-32)*(5/9). 5/9 is zero, since you're using integer division, so x will have the value 0.0 for all inputs. For floating-point division, use floating-point numbers (like 5.0/9.0).

Second, %d is for printing integers. Use %f to print floating-point values.

David Thornley