Abdulaziz Mukhamedov
Abdulaziz Mukhamedov

Reputation: 35

Change temperature F to C is having issues

I'm just started learning C programming. So I have problem with change temperature via Functions. Please check this program and where did I do mistake? Thank you!!!

 #include<stdio.h>
double f_to_c(double f);
double get_integer(void);

int main(void)
{
    double a;
    a = get_integer();

    printf("he degree in C:%f", f_to_c(a));

    return 0;
}
double get_integer(void)
{
    double n;
    printf("Enter the variable:");
    scanf_s("%f", &n);
        return n;
}
double f_to_c(double f)
{
    int f, c;
    c = 5.0 / 0.9*(f - 32.0);
    return c;
}
`

Upvotes: 0

Views: 89

Answers (3)

LPs
LPs

Reputation: 16213

Corrected code below:

#include<stdio.h>
double f_to_c(double f);
double get_integer(void);

int main(void)
{
    double a;
    a = get_integer();

    printf("he degree in C:%lf\n", f_to_c(a));

    return 0;
}
double get_integer(void)
{
    double n;
    printf("Enter the variable:");
    scanf("%lf", &n);
        return n;
}
double f_to_c(double f)
{
    double c;
    c = 5.0/9*(f-32);
    return c;
}

As you can see:

  1. Type of c variable in f_to_c is changed to double, because of you need a double return for that function
  2. Formula to convert F to C was not correct.
  3. Format specifier of your printf and scanf was not correct.

Upvotes: 0

amit dayama
amit dayama

Reputation: 3326

in get_integer function return type is double, instead it should be integer

int get_integer(void)
{
int n;
printf("Enter the variable:");
scanf_s("%d", &n);
    return n;
}

in your other function f_to_c, return type is double but you're returning an integer

double f_to_c(double f)
{
int f;
double c;
c = (5.0 / 0.9)*(f - 32.0);
return c;
}

also at the start, you need to change your code to:

int main(void)
{
int a;
a = get_integer();

printf("he degree in C:%f", f_to_c(a));

return 0;
}

Upvotes: 0

Sourav Ghosh
Sourav Ghosh

Reputation: 134286

In your case,

double f_to_c(double f)
{
    int f, c;
    c = 5.0 / 0.9*(f - 32.0);
    return c;
}

int f is shadowing the double f. Use some other name for the variable(s).

Essentially, you're trying to make use of an uninitialized automatic local variable which invokes undefined behavior

Upvotes: 2

Related Questions