Why does printf output random values for double and 0.000000 for int?

前端 未结 2 1529
小蘑菇
小蘑菇 2020-12-12 01:18

I know this is a simple questions, but it came up when I was coding and I am wondering how it works now. So, my first question is that when printf is given an integer like b

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-12 01:48

    You used the wrong format specifiers It should be

    int a = 2, b = 5, result = 0;
    result = b/a*a;
    
    printf("%d\n", result);
    
    ...
    
    double a = 2, b = 5, result = 0;
    result = b/a*a;
    
    printf("%f\n", result);
    

提交回复
热议问题