-2

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.

  • 5
    It probably crashes. You need `scanf("%f", &ce);`. Your compiler should have warned you about this. – John3136 Jan 20 '22 at 02:43
  • 3
    you should probably calculate `fh` AFTER you read in `ce`, too – Garr Godfrey Jan 20 '22 at 02:44
  • Apart from the missing `&` also see this newbie FAQ: https://stackoverflow.com/questions/4890480/c-program-to-convert-fahrenheit-to-celsius-always-prints-zero It should be 5.0/9.0. – Lundin Jan 20 '22 at 09:11

1 Answers1

-1

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.

TheMystZ
  • 80
  • 1
  • 8