STICCMAN
STICCMAN

Reputation: 1

Multiplying/Adding Doubles returns Errors

I wrote this program to convert Celsius to Fahrenheit, but the program only returns incorrect numbers. With how new I am to C or programming in general I have no idea what I should try to troubleshoot.

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

int main(void){

        double a;
        double b = a * 1.8;
        double c = b + 32;

        printf("Enter a temp in celcius");
        scanf("%lf", &a);
        printf("%f", &c);



        return 0;
}

Upvotes: 0

Views: 63

Answers (1)

Read the warnings that your compiler gives you:

q59587632.c:12:22: warning: format specifies type 'double' but the argument has
      type 'double *' [-Wformat]
        printf("%f", &c);
                ~~   ^~
q59587632.c:7:20: warning: variable 'a' is uninitialized when used here
      [-Wuninitialized]
        double b = a * 1.8;
                   ^
q59587632.c:6:17: note: initialize the variable 'a' to silence this warning
        double a;
                ^
                 = 0.0
2 warnings generated.

The first warning is because you're printing c's location in memory instead of its value. The second warning is because you're using a before you read it from the scanf. Those warnings aren't superfluous; they're the exact causes of your problem. Here's what your program looks like with both of those things fixed:

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

int main(void){

        double a;

        printf("Enter a temp in celcius");
        scanf("%lf", &a);

        double b = a * 1.8;
        double c = b + 32;

        printf("%f", c);



        return 0;
}

Upvotes: 5

Related Questions