printf

printf

功 能:

  产生格式化输出的函数

[编辑本段]

用 法:

  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.12345678;

  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");

  输出:

  123

  456

  789

  注意:

  printf("123/n" "456/n" "789/n");的输出结果与printf("123/n""456/n""789/n");相同。

  因此输出多行时,也并不需要每行调用一次printf。

例三

  妙用printf判断闰年程序

  #include<stdio.h>

  int main(void)

  {

  int a;

  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);

  产生下列输出:

  N_O

文件

  /usr/bin/printf 包含 printf 命令

printf

function

<cstdio>

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

format

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

n

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 the given field width; 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.

(space

)

If no sign is going to be written, a blank space is inserted before the

value.

#

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

(number

)

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

larger.

*

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).

l

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.

Example

123456789

10

11

12

/* printf example */

#include <stdio.h>

int main()

{

printf ("Characters: %c %c /n", 'a', 65);

printf ("Decimals: %d %ld/n", 1977, 650000L);

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.1416, 3.1416, 3.1416);

printf ("Width trick: %*d /n", 5, 10);

printf ("%s /n", "A string");

return 0;

}

13

14

15

And here is the output:

Characters: a A

Decimals: 1977 650000

Preceding with blanks: 1977

Preceding with zeros: 0000001977

Some different radixes: 100 64 144 0x64 0144

floats: 3.14 +3e+000 3.141600E+000

Width trick: 10

A string

See also

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)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值