printf 各种参数总结

来源:互联网 发布:耐思尼克域名注册平台 编辑:程序博客网 时间:2024/06/05 10:05

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
原创粉丝点击