C中的动态浮点格式说明符

有没有办法让用户输入浮点格式说明符? 例如,如果我打印这个。

float c = 15.0123 printf("%.2f", c); // outputs: 15.01 

如何为变量分配小数位数? 喜欢:

 int n = 3; float c = 15.0123 printf("%.(%i)f", n, c); // outputs: 15.012 

精度可以用带星号*的参数指定。 这称为参数提供的精度。

 float c = 15.0123; int m = 2; printf("%.*f", m, c); 

printf("%.*f", n, c); 将在小数点后的n个位置打印出c。