Why can't I change it?

Asked 2 years ago, Updated 2 years ago, 20 views

int c, f;

f=(int)((9.0/5)*c+32);

for(c=0;c<=50;c+=10)

printf("c=%d f=%d\n",c,f);

return0;

I squeezed it out, but 9.0/5 keeps coming out as zero

c++

2022-09-21 16:04

2 Answers

Try it as below.

float f = 9.0/5;
printf("f=%f\n", f);


2022-09-21 16:04

intc, f;

f=(int)((9.0/5)*c+32);

for(c=0;c<=50;c+=10)

printf("c=%d f=%d\n",c,f);

return0;

First of all, when putting the value in f, c is not initialized by the user The default value of c is 0, so f must be 32 in the printf statement.

intc{}, f{};
for(c=0;c<=50;c+=10) {
    f=(int)((9.0/5)*c+32);
    printf("c=%d f=%d\n",c,f);
}

And %d gets an int-type decimal. Then, the decimal point is naturally truncated, so if you want to express the decimal point, write %f.


2022-09-21 16:04

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.