user-0 提问: 求教 sprintf(str, "%50s",字符数组);

回复:
user: user-1
content: * An optional decimal integer specifying a minimum field width ./106/    If the converted value has fewer characters than the field width, it    will be padded with spaces on the left (or right, if the left    adjustment flag, described later, has been given) to the field width.   * An optional precision that gives the minimum number of digits to    appear for the d , i , o , u , x , and X conversions, the number of    digits to appear after the decimal-point character for e , E , and f    conversions, the maximum number of significant digits for the g and G    conversions, or the maximum number of characters to be written from a    string in s conversion.  The precision takes the form of a period (.)    followed by an optional decimal integer; if the integer is    omitted, it is treated as zero.

user: user-2
content: 引用 11 楼 zhao4zhong1 的回复:#include <stdio.h> char s2[]="ab"; char s7[]="1234567"; int main() {     printf("[%5s]\n",s2);     //[   ab]     printf("[%5s]\n",s7);     //[1234567]     printf("[%-5s]\n",s2);    //[ab   ]     printf("[%-5s]\n",s7);    //[1234567]     printf("[%.5s]\n",s2);    //[ab]     printf("[%.5s]\n",s7);    //[12345]     printf("[%5.1s]\n",s2);   //[    a]     printf("[%5.1s]\n",s7);   //[    1]     printf("[%-5.1s]\n",s2);  //[a    ]     printf("[%-5.1s]\n",s7);  //[1    ]     return 0; }  谢谢！

user: user-3
content: #include <stdio.h> char s2[]="ab"; char s7[]="1234567"; int main() {     printf("[%5s]\n",s2);     //[   ab]     printf("[%5s]\n",s7);     //[1234567]     printf("[%-5s]\n",s2);    //[ab   ]     printf("[%-5s]\n",s7);    //[1234567]     printf("[%.5s]\n",s2);    //[ab]     printf("[%.5s]\n",s7);    //[12345]     printf("[%5.1s]\n",s2);   //[    a]     printf("[%5.1s]\n",s7);   //[    1]     printf("[%-5.1s]\n",s2);  //[a    ]     printf("[%-5.1s]\n",s7);  //[1    ]     return 0; }

user: user-4
content: 我来汇总一下 %50s表示输出整个字符串，至少占50个字符的位置（当字符串长度超过50个字符时，这个数字被忽略），当不够50个字符时，在字符串前面用空格补齐（也就右对齐了）， %-50s表示当字符串不够50个字符时，在字符后面补空格（左对齐）， %.50s表示输出字符串的前50个字符，字符串长度不够50个字符串，此数字被忽略 %20.10s表示输出字符串的前10个字符，左侧用空格补齐，凑成20个字符 %-20.10s表示输出字符串的前10个字符，右侧用空格补齐，凑成20个字符 上面是输出字符串格式总结，对于输入字符串 %20s表示至多读入20个字符，多余字符被忽略，可用这种方式防止输入时内存溢出

user: user-5
content: 引用 6 楼 zhao4zhong1 的回复:对照我上面的回帖，谁免费解释一下 %-50s ？ 右边补空格嘛

user: user-3
content: 作为一个C程序员，对  scanf,sscanf,fscanf  printf,sprintf,fprintf 这类函数的用法，还是要做到“拳不离手，曲不离口”的。

user: user-3
content: 弄错了，上面我的回帖里面没提- Flag Directives The first optional field of the format specification is flags. A flag directive is a character that justifies output and prints signs, blanks, decimal points, and octal and hexadecimal prefixes. More than one flag directive may appear in a format specification.   Table R.4   Flag Characters  Flag Meaning Default  – Left align the result within the given field width.  Right align.  + Prefix the output value with a sign (+ or –) if the output value is of a signed type. Sign appears only for negative signed values (–).  0 If width is prefixed with 0, zeros are added until the minimum width is reached. If 0 and – appear, the 0 is ignored. If 0 is specified with an integer format (i, u, x, X, o, d) the 0 is ignored.  No padding.  blank (' ') Prefix the output value with a blank if the output value is signed and positive; the blank is ignored if both the blank and + flags appear. No blank appears.  # When used with the o, x, or X format, the # flag prefixes any nonzero output value with 0, 0x, or 0X, respectively. No blank appears.    When used with the e, E, or f format, the # flag forces the output value to contain a decimal point in all cases.  Decimal point appears only if digits follow it.    When used with the g or G format, the # flag forces the output value to contain a decimal point in all cases and prevents the truncation of trailing zeros.  Ignored when used with c, d, i, u, or s.  Decimal point appears only if digits follow it. Trailing zeros are truncated.

user: user-3
content: 对照我上面的回帖，谁免费解释一下 %-50s ？

user: user-6
content: %50s 最少50个字符 %.50s 最多50个字符

user: user-7
content: 引用 1 楼 slx_391987 的回复:%50  表示输出宽度。（可以理解成输出多少位） %.50表示输出精度（可以理解成有多少位小数） 看错了，没看到输出是 字符数组。2楼说的是对的。  如果输出float，double类型，则.（点号）表示精度

user: user-3
content: Format Specification Fields: printf and wprintf Functions A format specification, which consists of optional and required fields, has the following form:  %[flags] [width] [.precision] [{h | l | I64 | L}]type  printf Width Specification The second optional field of the format specification is the width specification. The width argument is a nonnegative decimal integer controlling the minimum number of characters printed. If the number of characters in the output value is less than the specified width, blanks are added to the left or the right of the values — depending on whether the – flag (for left alignment) is specified — until the minimum width is reached. If width is prefixed with 0, zeros are added until the minimum width is reached (not useful for left-aligned numbers).  The width specification never causes a value to be truncated. If the number of characters in the output value is greater than the specified width, or if width is not given, all characters of the value are printed (subject to the precision specification).  If the width specification is an asterisk (*), an int argument from the argument list supplies the value. The width argument must precede the value being formatted in the argument list. A nonexistent or small field width does not cause the truncation of a field; if the result of a conversion is wider than the field width, the field expands to contain the conversion result.  Precision Specification The third optional field of the format specification is the precision specification. It specifies a nonnegative decimal integer, preceded by a period (.), which specifies the number of characters to be printed, the number of decimal places, or the number of significant digits (see Table R.5). Unlike the width specification, the precision specification can cause either truncation of the output value or rounding of a floating-point value. If precision is specified as 0 and the value to be converted is 0, the result is no characters output, as shown below:  printf( "%.0d", 0 );      /* No characters output */  If the precision specification is an asterisk (*), an int argument from the argument list supplies the value. The precision argument must precede the value being formatted in the argument list.   The precision specifies the maximum number of characters to be printed. Characters in excess of precision are not printed. Characters are printed until a null character is encountered.

user: user-8
content: 引用 楼主 u012483962 的回复:求教 sprintf(str, "%50s",字符数组); 和 求教 sprintf(str, "%.50s",字符数组);    //50前多一个点 有什么区别吗？ 这个是把字符数组复制前50个字符到str吗？ 两者在字符串长度超过50的时候，差别就显现出来了 sprintf(str, "%50s",字符数组); 这个会显示超过50的字符 sprintf(str, "%.50s",字符数组);  这个不会显示超过50的字符，点号的意思就是只显示前50个

user: user-7
content: %50  表示输出宽度。（可以理解成输出多少位） %.50表示输出精度（可以理解成有多少位小数）