printf 各种参数总结

The syntax for a format placeholder is
%[parameter][flags][width][.precision][length]specifier

printf("example like %3$lf, %3$-10.5lf, %2$.5s, %1$-10d.\n", 123456, "abcdefghigk", m);


specifier

</pre><pre name="code" class="cpp">    
    // %d or %i
    unsigned int a = 2147483647 + 5;
    int b = -5;
    printf("signed decimal integer like %d, %i.\n", a, b);//wrong with a, ok with b
    
    // %u
    printf("unsigned decimal integer like %u, %u.\n", a, b);//wrong with b, ok with a
    
    // %o
    printf("unsigned octal like %o.\n", 100);
    
    // %x
    printf("unsigned hexadecimal integer like %x.\n", 100);
    
    // %f
    printf("decimal floating point, lowercase like %f.\n", 2.333f);
    
    // %F
    printf("decimal floating point, uppercase like %F.\n", 2.333f);
    
    // %e
    printf("scientific notation(mantissa/exponent), lowercase like %e.\n", 2e5);
    
    // %E
    printf("scientific notation(mantissa/exponent), uppercase like %E.\n", 2e5);
    
    // %g
    printf("use the shortest representation %%e or %%f like %g.\n", 234.334f);
    
    // %G
    printf("use the shortest representation %%E or %%F like %G.\n", 234.334f);
    
    // %a
    printf("hexadecimal floating point, lowercase like %a.\n", 1.2f);
    
    // %A
    printf("hexadecimal floating point, uppercase like %A.\n", 1.2f);
    
    // %c
    printf("character like %c.\n", a);
    printf("character like %c.\n", 'a');
    
    // %s
    printf("string like %s.\n", "aaaa");
    
    // %p
    char* buffer = (char*)malloc(sizeof(char) * 4);
    memset(&buffer, 0, sizeof(buffer));
    buffer = "aaa";
    printf("pointer address like %p.\n", buffer);
    
    // %n
    int* len = (int*)malloc(sizeof(int));
    *len = 4;
    printf("nothing printed like %n.\n", len);
    
    // %%
    printf("double %% to one %%.\n");

sub-specifier

parameter

// n$
    printf("to be output multiple times, using varying format specifiers or in different orders like %2$d, %1$d", 1, 2);
    printf("to be output multiple times, using varying format specifiers or in different orders like %1$#x, %1$#x", 16);

flags

// +
    printf("denote the sign of a number like %+d.\n", 2);
    printf("denote the sign of a number like %+d.\n", -2);
    
    // space
    printf("prefixed non-negative signed numbers with a space like % d.\n", 2);
    printf("prefixed non-negative signed numbers with a space like %    d.\n", 2);
    
    // -
    printf("left align the output like %10d.\n", 123321);
    printf("left align the output like %10d.\n", 321);
    printf("left align the output like %-10d.\n", 123321);
    printf("left align the output like %-10d.\n", 321);
    
    // 0
    printf("left align the output like %010d.\n", 321);
    
    // #
//    printf("hold the trailing zeros like %g.\n", 2.000f);
//    printf("hold the trailing zeros like %#g.\n", 2.000f);
//    printf("with a demical point like %f.\n", 2.f);
//    printf("with a demical point like %#f.\n", 2.f);

width

// (number)
    printf("Minimum number of characters to be printed like %3d.\n", 23423);
    printf("Minimum number of characters to be printed like %10d.\n", 23423);
    
    // *
    printf ("Width trick: %*d.\n", 5, 10);

precision

// .number
    printf("minimum number of digits to be written like %.3d.\n", 23423);
    printf("minimum number of digits to be written like %.10d.\n", 23423);
    printf("minimum number of digits to be written like %.10x.\n", 23423);
    printf("minimum number of digits to be written like %.5f.\n", 3.232323423);
    
    // .*
    printf("minimum number of digits to be written like %.*f.\n", 5, 3.232323423);

length

//length
    // l ll
    long d = 22;
    long long int e = 22;
    printf("long integer like %ld.\n", d);
    printf("long long integer like %lld.\n", e);
    
    // hh h
    char f = 'a';
    short g = 22;
    printf("signed char like %hhd.\n", f);
    printf("short int like %hd.\n", g);
    
    // z
    size_t t = 22;
    printf("size_t like %zd.\n", t);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值