Keil C51中通过printf函数进行串口调试

C51中的printf函数

keil→Help→μVision Help→C51 Development Tools→Cx51 Compiler Users' Guide→Library Reference→Include Files→stdio.h→printf
通过以上路径可以找到Keil C51帮助手册中printf函数的信息。


函数原型:

1
2
3
4
5
#include <stdio.h>

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

printf函数使用putchar函数构建一个字符串并写入输出流。
fmtstr是一个格式参数,可以由字符,转义序列和格式字符串组成。
格式字符串的格式为

1
% <[>flags<]> <[>width<]> <[>.precision<]> <[>{b|B|l|L}<]> type
Type Argument Type Input Format 描述
d int Signed decimal number 有符号整型数
u unsigned int Unsigned decimal number
o unsigned int Unsigned octal number 无符号八进制数
x unsigned int Unsigned hexadecimal number using "0123456789abcedf" 无符号十六进制数
X unsigned int Unsigned hexadecimal number using "0123456789ABCDEF" 无符号十六进制数
f float Floating-point number formatted as<[>-<]>dddd.dddd. float浮点型
e float Floating-point number formatted as <[>-<]>d.dddde<[>-<]>dd float浮点型
E float Floating-point number formatted as <[>-<]>d.ddddE<[>-<]>dd. float浮点型
g float Floating-point number using either the e or f format, whichever is more compact for the specified value and precision.
G float Floating-point number using either the E or f format, whichever is more compact for the specified value and precision.
c char A single character 单一字符
s * A string of characters terminated by a null character ('') 字符串
p * A generic pointer formatted as t:aaaa where t is the memory type and aaaa is the hexadecimal address 指针