1. C51中的printf函数
keil→Help→μVision Help→C51 Development Tools→Cx51 Compiler Users' Guide→Library Reference→Include Files→stdio.h→printf
通过以上路径可以找到Keil C51帮助手册中printf函数的信息。
函数原型:
#include
int printf (
const char *fmtstr /* format string */
<[>, arguments ... <]>); /* additional arguments */
printf函数使用putchar函数构建一个字符串并写入输出流。
fmtstr是一个格式参数,可以由字符,转义序列和格式字符串组成。
格式字符串的格式为
% <[>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. | 以%f或%e中较短的输出宽度输出单精度实数 |
G | float | Floating-point number using either the E or f format, whichever is more compact for the specified value and precision. | 以%f或%e中较短的输出宽度输出双精度实数 |
c | char | A single character | 输出单个字符 |
s | * | A string of characters terminated by a null character ('\0') | 输出字符串 |
p | * | A generic pointer formatted as t:aaaa where t is the memory type and aaaa is the hexadecimal address | 指针 |
字符 | 含义 | 描述 |
---|---|---|
b/B | char | b或B字符可以紧接在类型字符(d,i,u,o,x和X类型)之前,表示char类型。 |
l/L | long | l或L字符可以紧接在类型字符(d,i,u,o,x和X类型)之前,表示long类型。 |
比如
格式 | 含义 | 针对类型 |
---|---|---|
%d | 两个字节变量 | int |
%bd | 一个字节变量 | char |
%ld | 四个字节变量 | long int |
Flag | Description | 描述 |
---|---|---|
- | 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, respectively. When 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. | 对c、s、d、u类无影响;对o类,在输出时加前缀o;对x类,在输出时加前缀0x/0X;对e、E、g、G、f 类当结果有小数时才给出小数点。 |
#include
#include
int main()
{
char c, s[20];
int a=1234;
float f=3.141592653589;
double x=0.12345678912345678;
strcpy(s, "Hello,World");
c='\x41';
printf("a=%d\n", a);//按照十进制整数格式输出,显示 a=1234
printf("a=%d%%\n", a);//输出%号 结果 a=1234%
printf("a=%6d\n", a);//输出6位十进制整数 左边补空格,显示 a= 1234
printf("a=%06d\n", a);//输出6位十进制整数 左边补0,显示 a=001234
printf("a=%2d\n", a);//a超过2位,按实际输出 a=1234
printf("a=%-6d\n", a);///输出6位十进制整数 右边补空格,显示 a=1234
printf("f=%f\n", f);//浮点数有效数字是7位,结果 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.1234567891234567
printf("c=%c\n", c); //输出字符 c=A
printf("c=%x\n", c);//以十六进制输出字符的ASCII码 c=41
printf("s[]=%s\n", s);//输出数组字符串s[]=Hello,World
printf("s[]=%6.9s\n", s);//输出最多9个字符的字符串 s[]=Hello,Wor
return 0;
}
在CodeBlocks中运行结果如下
#include
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;
TI = 1;
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);
TI = 0;
在Keil C51中进行调试
Keil C51中printf函数的用法
printf函数是调用putchar函数输入的,查看Keil/C51/LIB/PUTCHAR.C可知putchar函数的源代码。
char putchar (char c) {
if (c == '\n') {
if (RI) {
if (SBUF == XOFF) {
do {
RI = 0;
while (!RI);
}
while (SBUF != XON);
RI = 0;
}
}
while (!TI);
TI = 0;
SBUF = 0x0d; /* output CR */
}
if (RI) {
if (SBUF == XOFF) {
do {
RI = 0;
while (!RI);
}
while (SBUF != XON);
RI = 0;
}
}
while (!TI);
TI = 0;
return (SBUF = c);
}
putchar函数是先判断TI是否为0,若TI为0,则在while (!TI);循环中等待TI非0,TI非0时将TI清0,然后送出一个字符。因此,在使用printf函数时需要先将TI置1再使用printf函数发送数据。
3. 通过串口打印printf函数输出信息
在Keil C51中进行调试时,通过View→Serial Windows→UART # 1可以打开串口调试窗口。在进行调试运行时,printf打印的信息就会显示在该窗口。