单片机printf详解

总结

#include <stdio.h>

int printf (
  const char *fmtstr         /* format string */
  <[>, arguments ... <]>);   /* additional arguments */

描述

printf函数格式化一系列字符串和数值,并使用putchar函数构建一个字符串以写入输出流。

fmtstr参数是一个格式字符串,可以由字符,转义序列和格式规范组成。

普通字符和转义序列按它们解释的顺序复制到流中。格式规范始终以百分号(’%’)开头,并且要求在printf函数调用中包括其他参数。

从左到右读取格式字符串。遇到的第一个格式规范引用了fmtstr之后的第一个参数,并使用格式规范对其进行转换和输出。

第二个格式规范访问fmtstr之后的第二个参数,依此类推。如果参数多于格式规范,则多余的参数将被忽略

如果没有足够的参数用于格式规范,或者参数类型与fmtstr指定的参数类型不匹配,则结果是不可预测的。

格式

格式规范具有以下常规格式:

% <[>flags<]> <[>width<]> <[>.precision<]> <[>{b|B|l|L}<]> type

格式规范中的每个字段可以是单个字符或指定特定格式选项的数字。

标志字段(flags)

标志字段是一个字符,用于证明输出的正当性并打印+/-符号和空白,小数点以及八进制和十六进制前缀,如下表所示。

FlagDescription翻译
-Left justify the output in the specified field width.左对齐指定字段宽度的输出。
+Prefix the output value with a + or - sign if the output is a signed type.如果输出是带符号类型,则在输出值前加上+或-前缀
blank(’’)Prefix the output value with a blank if it is a signed positive value. Otherwise, no blank is prefixed.如果输出值是有符号正值,则将其前缀为空白。 否则,不带空格前缀
#Prefixes a non-zero output value with 0, 0x, or 0X when used with o, x, and X field types,
respectivelyWhen used with the e, E, f, g, and G field types,the # flag forces the output value to include a decimal point.The # flag is ignored in all other cases.
当分别与o,x和X字段类型一起使用时,将非零输出值前缀为0、0x或0X。
与e,E,f,g和G字段类型一起使用时,#标志强制输出值包含小数点。
在所有其他情况下,#标志将被忽略。

宽度字段(width)

宽度字段是一个非负数,它指定最少要打印的字符数。 如果输出值中的字符数小于宽度,则在左侧(默认情况下)或右侧(指定-标志时)添加空格以填充到最小宽度。 如果width前缀为’0’,则填充零而不是空格。 width字段不会截断输出。 如果输出值的长度超过指定的宽度,则输出所有字符。

精度(.precision)

精度字段是一个非负数,它指定要打印的字符数,有效数字数或小数位数。 下表中指定了浮点数时,精度字段可能会导致输出值的截断或舍入。

TypePrecision Field Meaning翻译
d,u,o,x,XThe precision field specifies the minimum number of digits that are included in the output value. Digits are not truncated if the number of digits in the argument exceeds that defined in the precision field. If the number of digits in the argument is less than the precision field, the output value is padded on the left with zeros.精度字段指定输出值中包含的最小位数。 如果参数中的位数超过精度字段中定义的位数,则位数不会被截断。 如果参数中的位数小于precision字段,则在输出值的左侧填充零。
fThe precision field specifies the number of digits to the right of the decimal point. The last digit is rounded.精度字段指定小数点右边的位数。
最后一位四舍五入。
e.EThe precision field specifies the number of digits to the right of the decimal point. The last digit is rounded.精度字段指定小数点右边的位数。
最后一位四舍五入。
g,GThe precision field specifies the maximum number of significant digits in the output value.精度字段指定输出值中的最大有效位数。
sThe precision field specifies the maximum number of characters in the output value. Excess characters are not output.精度字段指定输出值中的最大字符数。
不输出多余的字符。
c,pThe precision field has no effect on these field types.精度字段对这些字段类型没有影响.

类型字段(type)

类型字段type是一个单个字符,用于指定将参数解释为字符,字符串,数字还是指针,如下表所示。

TypeArgument TypeInput Format翻译
dintSigned decimal number.有符号十进制
uunsigned intUnsigned decimal number.无符号十进制
ounsigned intUnsigned octal number.无符号八进制
xunsigned intUnsigned hexadecimal number using “0123456789abcedf”.无符号十六进制(小写)
Xunsigned intUnsigned hexadecimal number using “0123456789ABCDEF”.无符号十六进制(大写)
ffloatFloating-point number formatted as
<[>-<]>dddd.dddd.
浮点数
efloatFloating-point number formatted as
<[>-<]>d.dddde<[>-<]>dd.
科学计数法(e)
EfloatFloating-point number formatted as
<[>-<]>d.ddddE<[>-<]>dd.
科学计数法(E)
gfloatFloating-point number using either the e or f format, whichever is more compact for the specified value and precision.灵活选择e或者f的格式,
让数据的输出更加好看有效
GfloatFloating-point number using either the E or f format, whichever is more compact for the specified value and precision.灵活选择E或者f的格式,
让数据的输出更加好看有效
ccharA single character.一个字符(原始数据是什么就是什么)
s*A string of characters terminated by a null character (’\0’).一串以空字符(’\ 0’)结尾的字符。
p*A generic pointer formatted as t:aaaa where t is the memory type and aaaa is the hexadecimal address.通用指针,格式为t:aaaa,
其中t是内存类型,
而aaaa是十六进制地址。

错误示例

	 unsigned char a = 5;
	 Delay1000ms();
	 TI = 1;
	 printf("%d", a);	

在这里插入图片描述
这里不对的原因是因为格式化为为%d是有符号整形,而放上的数据为无符号字符型。一个是16位,而放上的是8位,在printf中会将8位左移形成16位发出。 因此使用printf必须注意类型!
在这里插入图片描述

检测示例

#include <stdio.h>

void tst_printf (void) {
  char a = 1;
  int b  = 12365;
  long c = 0x7FFFFFFF;

  unsigned char x = 'A';
  unsigned int y  = 54321;
  unsigned long z = 0x4A6F6E00;

  float f = 10.0;
  float g = 22.95;

  char buf [] = "Test String";
  char *p = buf;

  printf ("char %bd int %d long %ld\n",a,b,c);
  printf ("Uchar %bu Uint %u Ulong %lu\n",x,y,z);
  printf ("xchar %bx xint %x xlong %lx\n",x,y,z);
  printf ("String %s is at address %p\n",buf,p);
  printf ("%f != %g\n", f, g);
  printf ("%*f != %*g\n", (int)8, f, (int)8, g);
}

  • <[>-<]>表示符号
  • 可选字符l或L可以紧接在类型字符之前,以分别为d,i,u,o,x和X指定长类型。
  • 可选字符b或B可以紧接在类型字符之前,以分别为d,i,u,o,x和X指定char类型。
  • 百分号后的字符未被识别为格式规范,将被视为普通字符。 例如,“ %%”将单个百分号写入输出流。
  • 必须确保参数类型与格式规范的类型匹配。您可以使用类型转换来确保将正确的类型传递给printf。
  • 此函数是特定于实现的,基于_getkey和putchar函数的操作。标准库中提供的这些函数使用微控制器的串行端口读写字符。自定义函数可以使用其他I/O设备。
  • 由于8051施加的内存限制,可传递给该函数的字节总数受到限制。在小型或紧凑型中最多可以传递15个字节。在大型模型中最多可以传递40个字节。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一WILLPOWER一

你的鼓励是我创作最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值