-2

I am making a small program that converts a celsius temperature to Fahrenheit and Kelvin degrees It uses a function which takes a pointer to int as argument and returns Fahrenait.When the program is over i have to change the value of akc integer which is where i save the celsius temperature to kelvin degrees Here is what i have done.

float thermo(int *);
int main(){
    int akc;
    akc=100;
    printf("%dce = %f = %dK\n",akc,thermo(&akc),akc);
    system("pause");
    return 0;
}
float thermo(int *akc){
    float a=*akc;
    *akc+=273;
    return 9*a/5+32;
}

My problem is that when i print all the values i get the following output:

373 celsius = 212.000000 Fahrenheit = 100Kelvin

but the result should be

100 celsius = 212.000000 Fahrenheit = 373Kelvin

Any ideas?

1 Answers1

0
printf("%dce = %f = %dK\n",akc,thermo(&akc),akc);

The evaluation order of function argument is unspecified in C. You cannot assume the first argument will be evaluated, then the second, etc. To fix this you can save your results in temporary variables.

ouah
  • 142,963
  • 15
  • 272
  • 331