2
#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;
}

The code seems to be printing the address of variable f instead of the value; it's probably a syntax mistake.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
n0ob
  • 1,275
  • 8
  • 20
  • 23
  • 1
    See *http://stackoverflow.com/questions/210590/why-does-scanf-need-lf-for-doubles-when-printf-is-okay-with-just-f* for info on using **scanf()** and **printf()** with doubles – jschmier Feb 03 '10 at 16:15

6 Answers6

7

In the printf() calls, %d should be %f (or perhaps %.2f):

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

You also 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.

Also, the conversion specification for double with scanf() is %lf:

if (scanf("%lf", &f) != 1)
{
    fprintf(stderr, "Failed to read a valid floating point number\n");
    return(EXIT_FAILURE);
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
  • 1
    Use "%f" for both float and double; a float argument to a variadic function like printf is automatically promoted to double. – jschmier Feb 03 '10 at 16:07
3

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

dave4420
  • 46,404
  • 6
  • 118
  • 152
2

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;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user239556
  • 21
  • 3
2

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
  • 56,304
  • 9
  • 91
  • 158
0

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
  • 69,903
  • 20
  • 143
  • 156
0

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.
  • 44,016
  • 10
  • 73
  • 134