Home > Software design >  Why doesn't this program give the temperature in Fahrenheit? it just ends after asking for valu
Why doesn't this program give the temperature in Fahrenheit? it just ends after asking for valu

Time:01-20

This is the program:

#include<stdio.h>

int main()
{
    float ce;
    float fh = ((ce*9/5) 32);
    printf("Value of temperature in celcius: ");
    
    scanf("%f",ce);
    printf("value of temperature in farenheit is %f",fh);
    
}

The output is Value of temperature in celcius: 45

it just ends the program after i write the temperature.

CodePudding user response:

You need a & before ce, and calculate fh after you read ce

#include<stdio.h>

int main()
{
    float ce;
    printf("Value of temperature in celcius: ");
    
    scanf("%mf",&ce);
    float fh = ((ce*9/5) 32);
    printf("value of temperature in farenheit is %f",fh);
    
}

Try turn on compiler warnings, they should off told you about this.

The reason you need to is if a & is before a variable, it returned the location of that variable in physical memory.

The scarf function overrides the memory at that place, and thus you get the value of your variable in ce.

I wrote %mf and not %f to be buffer safe, it's a good habit to get in to.

  •  Tags:  
  • Related