Why can't I convert the automatic version of C language when it's really simple?

Asked 2 years ago, Updated 2 years ago, 131 views

#include <stdio.h>

int main(void)
{
    int c;
    float d = 5.6, e = 8.4;

    c = d + e;
    printf("c = d + e = %.2f \n", (float)c);

    return 0;
}

Isn't float d=5.6 and float=8.4 automatically converted to int, so it becomes 5 and 8, respectively? c=5+8=13 does not come out, and the value keeps coming out as 14...

casting int float

2022-09-20 11:06

1 Answers

In c=d+e;, the = operator has the lowest priority. Therefore, d+e is calculated first, and then the result is substituted for c. That is, 14.0 which is the sum of 5.6 and 8.4 is substituted for c.


2022-09-20 11:06

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.