How is conversion of float/double to int handled in printf?

前端 未结 7 1853
逝去的感伤
逝去的感伤 2020-11-30 07:56

Consider this program

int main()
{
        float f = 11.22;
        double d = 44.55;
        int i,j;

        i = f;         //cast float to int
        j          


        
7条回答
  •  悲哀的现实
    2020-11-30 08:49

    Because you are not using the float format specifier, try with:

    printf("i = %d, j = %d, f = %f, d = %f", i,j,f,d);
    

    Otherwise, if you want 4 ints you have to cast them before passing the argument to printf:

    printf("i = %d, j = %d, f = %d, d = %d", i,j,(int)f,(int)d);
    

提交回复
热议问题