Unexpected output of printf

前端 未结 4 2086
有刺的猬
有刺的猬 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:18

    int main()
    {
    
    int a=5;
    float b=3.5;
    printf("%f",b); //Use %f
    printf("\n%d",a); // Use %d
    
    }
    

    Refer: printf

    wrong format specifier to printf leads undefined behavior in most cases.

    However, since printf is a variadic function, and the arguments to variadic functions undergo the default argument cast. Like, a char is casted to an int.

提交回复
热议问题