user-0 提问: 格式化字符串

回复:
user: user-1
content: 引用 4 楼 zhao4zhong1 的回复: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  Each field of the format specification is a single character or a number signifying a particular format option. The simplest format specification contains only the percent sign and a type character (for example, %s). If a percent sign is followed by a character that has no meaning as a format field, the character is copied to stdout. For example, to print a percent-sign character, use %%.  The optional fields, which appear before the type character, control other aspects of the formatting, as follows:  type  Required character that determines whether the associated argument is interpreted as a character, a string, or a number (see Table R.3).  flags  Optional character or characters that control justification of output and printing of signs, blanks, decimal points, and octal and hexadecimal prefixes (see Table R.4). More than one flag can appear in a format specification.  width  Optional number that specifies the minimum number of characters output. (See printf Width Specification.)   precision  Optional number that specifies the maximum number of characters printed for all or part of the output field, or the minimum number of digits printed for integer values (see Table R.5).  h | l | I64 | L  Optional prefixes to type-that specify the size of argument (see Table R.6).膜拜！

user: user-2
content: 就是表示输出“字符串”的格式

user: user-3
content: 3楼+1  都是二进制，看你想解释成什么东西了。 %d是整数，%s是字符串，%c是字符，%x是。。。

user: user-4
content: 这是定义一个统一的标准，这样你的变量才知道怎么输出。 printf()是打印输出到终端，scanf()是获取终端输入

user: user-5
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  Each field of the format specification is a single character or a number signifying a particular format option. The simplest format specification contains only the percent sign and a type character (for example, %s). If a percent sign is followed by a character that has no meaning as a format field, the character is copied to stdout. For example, to print a percent-sign character, use %%.  The optional fields, which appear before the type character, control other aspects of the formatting, as follows:  type  Required character that determines whether the associated argument is interpreted as a character, a string, or a number (see Table R.3).  flags  Optional character or characters that control justification of output and printing of signs, blanks, decimal points, and octal and hexadecimal prefixes (see Table R.4). More than one flag can appear in a format specification.  width  Optional number that specifies the minimum number of characters output. (See printf Width Specification.)   precision  Optional number that specifies the maximum number of characters printed for all or part of the output field, or the minimum number of digits printed for integer values (see Table R.5).  h | l | I64 | L  Optional prefixes to type-that specify the size of argument (see Table R.6).

user: user-6
content: 因为计算机中的变量均是以二进制存储，而变量类型也只是规定了它们有无符号以及占多少字节，并没有规定他们输出是什么形式。。另外在具体问题中，使用者需要更具需要进行特定形式的输入输出，所以就产生了格式化输入输出。

user: user-7
content: 本来以为你说的是字符串的初始化，但看了1楼的回答感觉好像我错了 lz可能想知道printf（“%d”,i）里面这个“%d”是什么意思对么 这个“%d”是告诉printf按照什么格式去打印这个i 比如，%c就是按字符打印，%d就是按整形打印，%s就是按字符串打印 除了%d关键字以外的字符就照样输出

user: user-8
content: 简单说，你定义了一个整型变量i用来计算一个东西，最后需要打印一句话，比如一共有i个这样的东西，你用printf怎么输出呢？你不能这样printf("一共有"+i+"个");这里就需要把整型变量i格式化到输出字符串中，就需要这样printf("一共有%d个",i);