下程序输出的结果。请给详细步骤。。特别是这个printf输出格式 ("%08.4f,b")是什么意思另作说明。谢谢了

1332人阅读
  产生格式化输出的函数
[编辑本段]
  int printf(const char *format,[argument]);
  format 参数输出的格式,定义格式为:%[flags][width][.perc][F|N|h|l]type
  规定数据输出方式,具体如下:
  1.type 含义如下:
  d 有符号10进制整数
  i 有符号10进制整数
  o 无符号8进制整数
  u 无符号10进制整数
  x 无符号的16进制数字,并以小写abcdef表示
  X 无符号的16进制数字,并以大写ABCDEF表示
  F/f 浮点数
  E/e 用科学表示格式的浮点数
  g 使用%f和%e表示中的总的位数表示最短的来表示浮点数 G 同g格式,但表示为指数
  c 单个字符
  s 字符串
  % 显示百分号本身
  p 显示一个指针,near指针表示为:XXXX
  far 指针表示为:XXXX:YYYY
  n 相连参量应是一个指针,其中存放已写字符的个数
  2.flags 规定输出格式,取值和含义如下:
  无 右对齐,左边填充0和空格
  - 左对齐,右边填充空格
  + 在数字前增加符号 + 或 -
  0 将输出的前面补上0,直到占满指定列宽为止(不可以搭配使用-)
  空格 输出值为正时冠以空格,为负时冠以负号
  # 当type=c,s,d,i,u时没有影响
  type=o,x,X时,分别在数值前增加'0',"0x","0X"
  type=e,E,f时,总是使用小数点
  type=g,G时,除了数值为0外总是显示小数点
  3.width 用于控制显示数值的宽度,取值和含义如下
  n(n=1,2,3...) 宽度至少为n位,不够以空格填充
  0n(n=1,2,3...) 宽度至少为n位,不够左边以0填充
  * 格式列表中,下一个参数还是width
  4.prec 用于控制小数点后面的位数,取值和含义如下:
  无 按缺省精度显示
  0 当type=d,i,o,u,x时,没有影响
  type=e,E,f时,不显示小数点
  n(n=1,2,3...) 当type=e,E,f时表示的最大小数位数
  type=其他,表示显示的最大宽度
  .* 格式列表中,下一个参数还是width
  5.F|N|h|l 表示指针是否是远指针或整数是否是长整数
  F 远指针
  n 近指针
  h 短整数或单精度浮点数
  l 长整数或双精度浮点数
[编辑本段]
函数返回值:
  printf函数的一般形式为:
  int printf(const char *format,[argument]);
  以上形式,我们在Visual C++里输入&printf(&将会看到。
  说明printf函数类型为整型,其返回值是整型值。
  其值实际为printf控制输出的字符数。
  printf()函数实际上是将所有参数按字符输出,根据该函数的参数1(const char *format),我们不难理
  例如:
  int a,b;
  a=printf("gelin/n"); //a的值为6,
  b=printf("the value of printf is:%d",a); //b的值为24
  printf("/n%d/n",b);
  以上程序将会输出:
[编辑本段]
  #define C "gelin"
  main()
  int a=12345;
  float b=5.;
  char e,d,f;
  scanf("%c%c%c",&e,&d,&f);
  //分别演示:%d、%4d、%.4d、%d%*d%d
  printf("int is:%d/n",a);
  //分别演示:%d、%9d、%.9d、%8.4d、%-8.4d、%+8.4d
  printf("float is:%f/n",b);
  //分别演示:%f、%8f、%.4f、%8.4f、%-8.4f
  printf("char is:%s/n",C);
  //分别演示:%s、%8s、%.4s、%8.4s、%-8.4s
  printf也可以这样用:
  printf("123/n""456/n""789/n");
  输出:
  注意:
  printf("123/n" "456/n" "789/n");的输出结果与printf("123/n""456/n""789/n");相同。
  因此输出多行时,也并不需要每行调用一次printf。
  妙用printf判断闰年程序
  #include&stdio.h&
  int main(void)
  scanf("%d",&a);
  printf(a%(a%100?4:400)?"NO":"YES");
  return 0;
  #include &stdio.h&
  #include&string.h&
  int main()
  char ch[20];
  int m,n;
  strcpy(ch,"Happy!");
  scanf("%d%d",&m,&n);
  printf("%*.*s/n",m,n,ch);
  return 0;
  其中前边*定义的是总的宽度,后边*是指定输出字符个数。分别对应外边参数m和n。
  输入: 10 3
  输出: Hap
[编辑本段]
printf 命令
  写格式化输出。
  printf Format [ Argument ... ]
  printf 命令转换、格式化并写 Argument 参数到标准输出。Argument 参数是由 Format 参数控制格式化
的。格式化输出行不能超出 LINE_MAX 字节长度。
  下列环境变量影响 printf 命令的执行:
  LANG 在 LC_ALL 和相应的环境变量(以 LC_ 开头)没有指定语言环境时,确定语言环境编目使用的
语言环境。
  LC_ALL 确定用于覆盖由 LANG 或其它任何 LC_ 环境变量设置的任何语言环境编目值的语言环境。
  LC_CTYPE 确定把文本字节数据顺序解释为字符的语言环境;例如,单一字节对应多字节字符的参数。
  LC_MESSAGES 确定写消息使用的语言。
  LC_NUMERIC 确定数字格式编排的语言环境。此环境变量影响使用 e、E、f、g 和 G 转换字符编写的
数字的格式。
  Format 参数是包含三种对象类型的一个字符串:
  * 无格式字符复制到输出流。
  * 转换规范,每个规范导致在值参数列表中检索 0 个或更多个项。
  * 以下转义序列。在复制到输出流时,这些序列导致它们的相关操作在有此功能的设备上显示:
  // 反斜杠
  /a 警告
  /b 退格
  /f 换页
  /n 换行
  /r 回车
  /t 跳格
  /v 垂直跳格
  /ddd ddd 是 1、2 或 3 位八进制数字。这些转义序列作为由八进制数指定的具有数字值的字节显示。
  Argument 参数是一个或多个字符串的列表,它在 Format 参数的控制下被写到标准输出。
  Format 参数在必要的情况下会经常重新使用以满足 Argument 参数。将好像提供了空字符串 Argument
一样评估任何额外的 c 或者 s 转换规范;其它额外转换规范将好像提供了 0 Argument 一样评估。此处
Format 参数不包含转换规范仅出现 Argument 参数,结果是不确定的。
语法详细介绍
  每个 Format 参数中的转换规范都具有如下顺序的语法:
  1. % (百分号)。
  2. 零或更多的选项,修改转换规范的含义。选项字符和它们的含义是:
  - 转换结果在字段中左对齐。
  + 符号转换结果常以符号(+ 或者 -)开始。
  空格 如果符号转换的第一个字符不是符号,结果的前缀将是空格。如果空格和 + 选项字符都显示,则忽
略空格选项字符。
  # 此选项指定值转换到备用格式。对于 c、d、i, u 和 s 转换,选项没有作用。对于 o 转换,它增加精度
来强制结果的第一数字是 a、0(零)。对于 x 和 X 转换,非零结果分别具有 0x 或 0X 前缀。对于 e、E、
f、g 和 G 转换,结果通常包含基数字符,即使基数字符后没有数字。对于 g 和 G 转换,结尾零不象通常一
  0 对于 d、i、o、 u、x、e、 E、f、g 和 G 转换,前导零(跟在符号或底数的后面)用于填充字段宽度,
将不用空格填充。如果显示 0(零)和 -(减号)选项,0(零)选项被忽略。对于 d、i、o、u、x 和 X 转换,
如果指定精度,0(零)选项将被忽略。
  其它转换,没有定义其行为。
  3. 可选的指定最小值字段宽度的十进制数字字符串。如果转换值字符少于字段宽度,该字段将从左到右
按指定的字段宽度填充。如果指定了左边调整选项,字段将在右边填充。如果转换结果宽于字段宽度,将扩
展该字段以包含转换后的结果。不会发生截断。然而,小的精度可能导致在右边发生截断。
  4. 可选的精度。精度是一个 .(点)后跟十进制数字字符串。如果没有给出精度,按 0(零)对待。精
  * d、o、i、 u、x 或 X 转换的最少数字显示位数。
  * e 和 f 转换的基数字符后的最少数字显示位数。
  * g 转换的最大有效数字位数。
  * s 转换中字符串的最大打印字节数目。
  5. 指示要应用的转换类型的一个字符,例如:
  % 不进行转换。打印一个 %(百分号)。
  d, i 接受整数值并将它转换为有符号的十进制符号表示法。精度指定显示的最小数字位数。如果值转换
后可以用更少的位数来表示,将使用前导零扩展。缺省精度是 1。精度为零的零值转换的结果是空字符串。
用零作为前导字符来指定字段宽度,导致用前导零填充字段宽度值。
  o 接受整数值并将它转换为有符号的八进制符号表示法。精度指定显示的最小数字位数。如果值转换后
可以用更少的位数来表示,将使用前导零扩展。缺省精度是 1。精度为零的零值转换的结果是空字符串。用
零作为前导字符来指定字段宽度,导致用前导零填充字段宽度值。不用八进制值表示字段宽度。
  u 接受整数值并将它转换为无符号的十进制符号表示法。精度指定显示的最小数字位数。如果值转换后
可以用更少的位数来表示,将使用前导零扩展。缺省精度是 1。精度为零的零值转换的结果是空字符串。用
零作为前导字符来指定字段宽度,导致用前导零填充字段宽度值。
  x, X 接受整数值并将它转换为十六进制符号表示法。字母 abcdef 用于 x 转换,字母 ABCDEF 用于 X 转
换。精度指定显示的最小数字位数。如果值转换后可以用更少的位数来表示,将使用前导零扩展。缺省精度
是 1。精度为零的零值转换的结果是空字符串。用零作为前导字符来指定字段宽度,导致用前导零填充字段
  f 接受浮点或者双精度值并将它转换为十进制符号表示法,格式为 [-] ddd.ddd。基数字符(在这里显示
为十进制点)后的数字位数等于规定的精度。 LC_NUMERIC 语言环境编目确定在这个格式中使用的基数字
符。如果不指定精度,则输出六个数字。如果精度是 0(零),将不显示基数字符。
  e, E 接受浮点或者双精度值并将它转换为指数表示的形式 [-] d.dde{+|-}dd。在基数字符前有一个数字
(在这里显示为十进制点),基数字符后的数字位数等于规定的精度。 LC_NUMERIC 语言环境编目确定在
这个格式中使用的基数字符。如果不指定精度,则输出六个数字。如果精度是 0(零),将不显示基数字符。
E 转换字符在指数前生成带 E 而不是带 e 的数字。指数通常至少包含两个数字。然而,如果要打印的指数值
大于两个数字,必要时需要打印附加指数数字。
  g、G 接受浮点和双精度值并转换为 f 或 e 转换字符的样式(或在 G 转换的情况下是 E),用精度指定
有效数字的个数。尾零将从结果中除去。基数字符只有在其后是数字时显示。使用的样式取决于转换的值。
样式 g 仅在转换的指数结果小于 -4,或大于或等于精度时使用。
  c 接受值将其作为字符串并打印字符串中的第一个字符。
  s 接受值将其作为字符串并打印字符串中的字符直到字符串结束或者达到精度指示的字符个数。如果没
有指定精度,打印全部字符直到出现第一个空字符。
  b 接受值将其作为字符串,可能包含反斜杠转义序列。打印来自转换字符串的字节直到字符串结束或者
达到精度规范指示的字节数。如果没有指定精度,打印全部字节直到出现第一个空字符。
  支持下列反斜杠转义序列:
  * 先前列出的反斜杠转义序列在 Format 参数描述下。这些转义序列将被转换到它们表示的单个字符。
  * /c(反斜杠 c)序列,它不显示并使 printf 命令忽略 Format 参数中的字符串参数包含的剩余的所有字
符串,所有剩余的字符串参数和所有附加字符。
  退出状态
  该命令返回以下出口值:
  0 成功完成。
  &0 发生错误。
  1. 输入下列命令:
  printf ("%5d%4d/n",1213,43);
  产生下列输出:
  _1213_ _43
  三次使用 Format 参数打印所有给定字符串。0(零)由 printf 命令提供以满足最后的 %4d 转换规格。
  2. 输入下列命令
  printf ("%c %c/n",78,79);
  产生下列输出:
  /usr/bin/printf 包含 printf 命令
int printf ( const char * format, ... );
Print formatted data to stdout
Writes to the standard output (stdout) a sequence of data formatted as
the format argument specifies. After the format parameter, the function expects
at least as many additional arguments as specified informat.
Parameters
String that contains the text to be written to stdout.
It can optionally contain embedded format tags that are substituted by the values specified in
subsequent argument(s) and formatted as requested.
The number of arguments following the format parameters should at least be as much as the number
of format tags.
The format tags follow this prototype:
%[flags][width][.precision][length]specifier
Where specifier is the most significant one and defines the type and the interpretation of the value of
the coresponding argument:
specifier Output Example
c Character a
d or i Signed decimal integer 392
e Scientific notation (mantise/exponent)
using e character 3.9265e+2
E Scientific notation (mantise/exponent)
using E character 3.9265E+2
f Decimal floating point 392.65
g Use the shorter of %e or %f 392.65
G Use the shorter of %E or %f 392.65
o Signed octal 610
s String of characters sample
u Unsigned decimal integer 7235
x Unsigned hexadecimal integer 7fa
X Unsigned hexadecimal integer (capital letters) 7FA
p Pointer address B800:0000
Nothing printed. The argument must be a pointer to a
signed int, where the number of characters written
so far is stored.
% A % followed by another % character will
write % to stdout.
The tag can also contain flags, width, .precision and modifiers sub-specifiers, which are optional and
follow these specifications:
flags description
- Left-justify within t Right justification is the
default (see width sub-specifier).
Forces to precede the result with a plus or minus sign (+ or -) even
for positive numbers. By default, only negative numbers are
preceded with a - sign.
If no sign is going to be written, a blank space is inserted before the
Used with o, x or X specifiers the value is preceeded
with 0, 0x or 0X respectively for values different than zero.
Used with e, E and f, it forces the written output to contain a
decimal point even if no digits would follow. By default, if no digits
follow, no decimal point is written.
Used with g or G the result is the same as with e or E but trailing
zeros are not removed.
0 Left-pads the number with zeroes (0) instead of spaces, where
padding is specified (see width sub-specifier).
width description
Minimum number of characters to be printed. If the value to be
printed is shorter than this number, the result is padded with
blank spaces. The value is not truncated even if the result is
The width is not specified in the format string, but as an
additional integer value argument preceding the argument that
has to be formatted.
precision description
.number For integer specifiers (d, i, o, u, x, X): precision specifies the
minimum number of digits to be written. If the value to be
written is shorter than this number, the result is padded with
leading zeros. The value is not truncated even if the result is
longer. A precision of 0 means that no character is written for
the value 0.
For e, E and f specifiers: this is the number of digits to be
printed after the decimal point.
For g and G specifiers: This is the maximum number of
significant digits to be printed.
For s: this is the maximum number of characters to be printed.
By default all characters are printed until the ending null
character is encountered.
For c type: it has no effect.
When no precision is specified, the default is 1. If the period is
specified without an explicit value for precision, 0 is assumed.
The precision is not specified in the format string, but as an
additional integer value argument preceding the argument that
has to be formatted.
length description
h The argument is interpreted as a short int or unsigned short
int (only applies to integer specifiers: i, d, o, u, x and X).
The argument is interpreted as a long int or unsigned long
int for integer specifiers (i, d, o, u, x and X), and as a wide
character or wide character string for specifiers c and s.
L The argument is interpreted as a long double (only applies to
floating point specifiers: e, E, f, g and G).
additional arguments
Depending on the format string, the function may expect a sequence of additional arguments, each
containing one value to be inserted instead of each %-tag specified in the format parameter, if any.
There should be the same number of these arguments as the number of %-tags that expect a value.
Return Value
On success, the total number of characters written is returned.
On failure, a negative number is returned.
/* printf example */
#include &stdio.h&
int main()
printf ("Characters: %c %c /n", 'a', 65);
printf ("Decimals: %d %ld/n", L);
printf ("Preceding with blanks: %10d /n", 1977);
printf ("Preceding with zeros: %010d /n", 1977);
printf ("Some different radixes: %d %x %o %#x %#o /n", 100, 100, 100, 100, 100);
printf ("floats: %4.2f %+.0e %E /n", 3.6, 3.1416);
printf ("Width trick: %*d /n", 5, 10);
printf ("%s /n", "A string");
And here is the output:
Characters: a A
Preceding with blanks: 1977
Preceding with zeros:
Some different radixes: 100 64 144 0x64 0144
floats: 3.14 +3e+000 3.0
Width trick: 10
puts Write string to stdout (function)
scanf Read formatted data from stdin (function)
fprintf Write formatted output to stream (function)
fwrite Write block of data to stream (function)
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:57615次
排名:千里之外
原创:20篇
转载:39篇
(4)(5)(11)(14)(25)遇到printf输出问题
遇到printf输出问题
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& -------------------------------------------------------Sun---------------------------------------------------------------------------
在牛客遇到此问题:
int main()
&&& float b,c;
&&& scanf(&%2d%3f%4f&,&a,&b,&c);
&&& printf(&\na=%d,b=%d,c=%f\n&,a,b,c);
&&& return 0;
输入l,结果是:
codeblocks下
Linux下&&&&&&&&&
总之和编译器有关啦(⊙o⊙)…
补充基础知识:
一、printf()函数&&
& printf()函数是格式化输出函数,&一般用于向标准输出设备按规定格式输出信息。在编写程序时经常会用到此函数。
& printf()函数的调用格式为:& printf(&&格式化字符串&&,&&参量表&);&&
& 其中格式化字符串包括两部分内容:&一部分是正常字符,&这些字符将按原样输出;&另一部分是格式化规定字符,&以&%&开始,&后跟一个或几个规定字符,& 用来确定输出内容格式。&&
& 参量表是需要输出的一系列参数,&其个数必须与格式化字符串所说明的输出参数个数一样多,&各参数之间用&,&分开,&且顺序一一对应,&否则将会出现意想不到的错误。
1.&格式化规定符&&
%d&十进制有符号整数&&
%u&十进制无符号整数&&
%f&浮点数&&
%s&字符串&&
%c&单个字符&&
%p&指针的值&&
%e&指数形式的浮点数&&
%x,&%X&无符号以十六进制表示的整数&&
%0&无符号以八进制表示的整数&&
%g&自动选择合适的表示法&&
(1).&可以在&%&和字母之间插进数字表示最大场宽。&&
例如:&%3d&表示输出3位整型数,&不够3位右对齐。& %9.2f&表示输出场宽为9的浮点数,&其中小数位为2,&整数位为6, 小数点占一位,&不够9位右对齐。&&
&&&&&&&&& %8s&表示输出8个字符的字符串,&不够8个字符右对齐。&&
&&&&&&&&& 如果字符串的长度、或整型数位数超过说明的场宽,&将按其实际长度输出。但对浮点数,&若整数部分位数超过了说明的整数位宽度,&将按实际整数位输出;& 若小数部分位数超过了说明的小数位宽度,&则按说明的宽度以四舍五入输出。 另外,&若想在输出值前加一些0,&就应在场宽项前加个0。&&
例如:&%04d&表示在输出一个小于4位的数值时,&将在前面补0使其总宽度为4位。&
如果用浮点数表示字符或整型量的输出格式,&小数点后的数字代表最大宽度,& 小数点前的数字代表最小宽度。&&
例如:&%6.9s&表示显示一个长度不小于6且不大于9的字符串。若大于9,&则第9个字符以后的内容将被删除。
(2).&可以在&%&和字母之间加小写字母l,&表示输出的是长型数。&&
例如:&%ld&表示输出long整数& %lf&表示输出double浮点数&
(3).&可以控制输出左对齐或右对齐,&即在&%&和字母之间加入一个&-&&号可说明输出为左对齐,&否则为右对齐。&&
例如:&%-7d&表示输出7位整数左对齐&&
&&&&&&&&& %-10s&表示输出10个字符左对齐&
2.&一些特殊规定字符&
\f&清屏并换页&&
\t&Tab符&&
\xhh&表示一个ASCII码用16进表示,&&
其中hh是1到2个16进制数&&
int&main()&
&&& char&c,&s[20],&*p;&
&&& int&a=1234,&*i;&
&&& float&f=3.;&
&&& double&x=0.54321;&
&&& p=&How&do&you&do&;&
&&& strcpy(s,&&Hello,&Comrade&);&
&&& *i=12;&
&&& c='\x41';&
&&& printf(&a=%d\n&,&a);&/*结果输出十进制整数a=1234*/&
printf(&a=%6d\n&,&a);&/*结果输出6位十进制数a=&1234*/&
printf(&a=%06d\n&,&a);&/*结果输出6位十进制数a=001234*/&
printf(&a=%2d\n&,&a);&/*a超过2位,&按实际值输出a=1234*/&
printf(&*i=%4d\n&,&*i);&/*输出4位十进制整数*i=&12*/&
printf(&*i=%-4d\n&,&*i);&/*输出左对齐4位十进制整数*i=12*/&
printf(&i=%p\n&,&i);&/*输出地址i=06E4*/&
printf(&f=%f\n&,&f);&/*输出浮点数f=3.141593*/&
printf(&f=6.4f\n&,&f);&/*输出6位其中小数点后4位的浮点数 f=3.1416*/&
printf(&x=%lf\n&,&x);&/*输出长浮点数x=0.123457*/&
printf(&x=%18.16lf\n&,&x);/*输出18位其中小数点后16位的长浮点数x=0.5432*/&
printf(&c=%c\n&,&c);&/*输出字符c=A*/&
printf(&c=%x\n&,&c);&/*输出字符的ASCII码值c=41*/&
printf(&s[]=%s\n&,&s);&/*输出数组字符串s[]=Hello,&Comrade*/&
printf(&s[]=%6.9s\n&,&s);/*输出最多9个字符的字符串s[]=Hello, Co*/&
&&& printf(&s=%p\n&,&s);&/*输出数组字符串首字符地址s=FFBE*/&
&&& printf(&*p=%s\n&,&p);&/*&输出指针字符串p=How&do&you&do*/&
&&& printf(&p=%p\n&,&p);&/*输出指针的值p=0194*/&
&&& getchar();&
&&& retunr&0;&
二、scanf()函数&
& scanf()函数是格式化输入函数,&它从标准输入设备(键盘)&读取输入的信息。&
& 其调用格式为:&
& scanf(&&格式化字符串&&,&&地址表&);&
& 格式化字符串包括以下三类不同的字符;&
1.&格式化说明符:&格式化说明符与printf()函数中的格式说明符基本相同。&
2.&空白字符:&空白字符会使scanf()函数在读操作中略去输入中的一个或多个空白字符。&
3.&非空白字符:&一个非空白字符会使scanf()函数在读入时剔除掉与这个非空白字符相同的字符。
地址表是需要读入的所有变量的地址,&而不是变量本身。这与printf()函数完全不同,&要特别注意。各个变量的地址之间同&,&分开。
int main()&
int&i,&j;&
printf(&i,&j=?\n&);&
scanf(&%d,&%d&,&&i,&&j);&
上例中的scanf()函数先读一个整型数,&然后把接着输入的逗号剔除掉,&最后读入另一个整型数。如果&,&这一特定字符没有找到,&scanf()函数就终止。若参数之间的分隔符为空格,&则参数之间必须输入一个或多个空格。&
(1).&对于字符串数组或字符串指针变量,&由于数组名和指针变量名本身就是地址,&因此使用scanf()函数时,&不需要在它们前面加上&&&操作符。
int main()
char&*p,&str[20];&
scanf(&%s&,&p);&/*从健盘输入字符串*/&
scanf(&%s&,&str);&
printf(&%s\n&,&p);&/*向屏幕输出字符串*/&
printf(&%s\n&,&str);&
(2).&可以在格式化字符串中的&%&各格式化规定符之间加入一个整数,&表示任何读操作中的最大位数。&
如例3中若规定只能输入10字符给字符串指针p,&则第一条scanf()&函数语句变为scanf(&%10s&,&p);&
程序运行时一旦输入字符个数大于10,&p就不再继续读入,&而后面的一个读入函数即scanf(&%s&,&str)就会从第11个字符开始读入。&
实际使用scanf()函数时存在一个问题,&下面举例进行说明: 当使用多个scanf()函数连续给多个字符变量输入时,&例如:&
int main()&
char&c1,&c2;&
scanf(&%c&,&&c1);&
scanf(&%c&,&&c2);&
printf(&c1&is&%c,&c2&is&%c&,&c2\1,&c2);&
运行该程序,&输入一个字符A后回车&(要完成输入必须回车),&在执行scanf (&%c&,&&c1)时,&给变量c1赋值&A&,&但回车符仍然留在缓冲区内,&执行输入语句&
scanf(&%c&,&&c2)时,&变量c2输出的是一空行,&如果输入AB后回车,&那么输出结果为:&c1&is&A,&c2&is&B。&
要解决以上问题,&可以在输入函数前加入清除函数fflush()(&这个函数的使用方法将在本节最后讲述)。修改以上程序变成:&
char&c1,&c2;&
scanf(&%c&,&&c1);&
fflush(stdin);&
scanf(&%c&,&&c2);&
printf(&c1&is&%c,&c2&is&%c&,&c1,&c2);&
非格式化输入输出函数&
非格式化输入输出函数可以由上面讲述的标准格式化输入输出函数代替,&但这些函数编译后代码少,&相对占用内存也小,&从而提高了速度,&同时使用也比较方便。
一、puts()和gets()函数&
1.&puts()函数&
puts()函数用来向标准输出设备(屏幕)写字符串并换行,&其调用格式为: puts(s);&
其中s为字符串变量(字符串数组名或字符串指针)。 puts()函数的作用与语printf(&%s\n&,&s)相同。&
char&s[20],&*f;&/*定义字符串数组和指针变量*/&
strcpy(s,&&Hello!&);&/*字符串数组变量赋值*/&
f=&Thank&you&;&/*字符串指针变量赋值*/&
(1).&puts()函数只能输出字符串,&不能输出数值或进行格式变换。&
(2).&可以将字符串直接写入puts()函数中。如:&
puts(&Hello,&Turbo&C2.0&);&
2.&gets()函数&
gets()函数用来从标准输入设备(键盘)读取字符串直到回车结束,&但回车符不属于这个字符串。其调用格式为: gets(s);&
其中s为字符串变量(字符串数组名或字符串指针)。&
gets(s)函数与scanf(&%s&,&&s)相似,&但不完全相同,&使用scanf(&%s&,&&s) 函数输入字符串时存在一个问题,&就是如果输入了空格会认为输入字符串结束, 空格后的字符将作为下一个输入项处理,&但gets()&函数将接收输入的整个字符串直到回车为止。
char&s[20],&*f;&
printf(&What's&your&name?\n&);&
gets(s);&/*等待输入字符串直到回车结束*/&
puts(s);&/*将输入的字符串输出*/&
puts(&How&old&are&you?&);&
我的热门文章
即使是一小步也想与你分享}

我要回帖

更多关于 printf输出格式 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信