printf()的*修饰符:
printf()的*修饰符的用法:
如果你不想预先指定字段宽度或精度,希望通过程序来指定,那么可以通过*修饰符代替字段宽度。但还是要用一个参数告诉函数,字段宽度应该是多少。
#include<stdio.h>
int main()
{
int number = 123;
float cost = 9.98;
unsigned width,precision;
printf("PleaseEnter a field width:\n");
scanf("%d",&width);
printf("%*d\n", width, number);
printf("Nowplease enter a width and a precision:\n");
scanf("%d%d", &width, &precision);
printf("$%*.*f\n", width, precision, cost);
return 0;
}
输出:
请读者上机亲自运行,这样才会印象深刻!
原文地址:https://www.cnblogs.com/Xiangzhong-com/p/9102545.html
时间: 2024-10-11 08:54:22