printf

碰到了printf的一个很奇葩的用法。

A width or precision may be specified as *, in which case the value is compputed by converting the next argument (which must be an int). For example, to print at most max characters from a string s.

#include <stdio.h>  
#include <cstring.h>  

int main() {  
    int max;  
    char *s = "this is a test!
";  

    scanf("%d", &max);  
    printf("%.*s

", max, s);  

    return 0;  
}

提示:自己手动输入值,试试

再来一个例子

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main() {
    int max, d;

    scanf("%d%d", &d, &max);
    printf("%0*d
", max, d);

    return 0;
}

input

3 5

output

00003

这篇文章用markdown写的哦,感觉还不错

原文地址:https://www.cnblogs.com/tanhehe/p/3250859.html