Unexpected output of printf

前端 未结 4 2089
有刺的猬
有刺的猬 2020-11-30 15:15
int a=5;
float b=3.5;
printf(\"%d\",b);
printf(\"\\n%f\",a);

Can anyone please tell me why this code is showing unexpected output (garbage\\n3.5)

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-30 16:06

    Because the a variable is a int type, while your are specifying a format string for a float type, and vice versa for the variable b.

    Note :

    %d is for integer type

    %f is for float type

    You should use :

    int a=5;
    float b=3.5;
    printf("%f",b);
    printf("\n%d",a);
    

提交回复
热议问题