C语言知识点八: printf()的*修饰符

printf()的*修饰符:

printf()的*修饰符的用法:

如果你不想预先指定字段宽度或精度,希望通过程序来指定,那么可以通过*修饰符代替字段宽度。但还是要用一个参数告诉函数,字段宽度应该是多少。

示例:

#include<stdio.h>

int main()

{

    int number = 123;

    float cost = 9.98;

    unsigned width,precision;

    printf("PleaseEnter a field width:\n");

扫描二维码关注公众号,回复: 1784885 查看本文章

    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;

}

输出:


请读者上机亲自运行,这样才会印象深刻!

猜你喜欢

转载自blog.csdn.net/weixin_41588502/article/details/80489957