转自:http://blog.csdn.net/zhanglianpin/article/details/44779009

在Keil C51 中使用printf ,首先需要重新实现 putchar(char c)函数。此函数在

char putchar (char c)
{
ES=0;
SBUF = c;
while(TI==0);
TI=0;
ES=1;
return 0;
}

我们先分析一下上面这个程序哈, 
关闭串口中断 
发送单字节数据 
等待发送完毕 
清除TI标志 
开启串口中断

在main函数里可以直接使用printf函数进行输出了。 
但是,我一直存在这样一个疑惑:

void main()
{
unsigned char test1 = 55;
printf("the test is %d\r\n",test1);
}

使用串口输出的数值一直不对,我后来自己理解,%d是整型,而在Keil C51整型占用2个byte,所以我一般的解决办法是做一次强制类型转换:

void main()
{
unsigned char test1 = 55;
printf("the test is %d\r\n",(int)test1);
}

后来阅读Keil C51的帮助手册: 
得到这样一条信息:

格式 含义 针对类型
%d 两个字节变量 int
%bd 单字节变量 char
%ld 四字节变量 long int

所以上面的问题的另一个解决方案是:

void main()
{
unsigned char test1 = 55;
printf("the test is %bd\r\n",test1);
}

下面附上Keil C51手册内容。 
int printf (

const char fmtstr / format string */ 
<[>, arguments … <]>); /* additional arguments */

Description The printf function formats a series of strings and numeric values and builds a string to write to the output stream using the putchar function. The fmtstr argument is a format string that may be composed of characters, escape sequences, and format specifications.

Ordinary characters and escape sequences are copied to the stream in the order in which they are interpreted. Format specifications always begin with a percent sign (‘%’) and require that additional arguments are included in the printf function call.

The format string is read from left to right. The first format specification encountered references the first argument after fmtstr and converts and outputs it using the format specification. The second format specification accesses the second argument after fmtstr, and so on. If there are more arguments than format specifications, extra arguments are ignored. Results are unpredictable if there are not enough arguments for the format specifications or if the argument types do not match those specified by fmtstr.

Format specifications have the following general format:

% <[>flags<]> <[>width<]> <[>.precision<]> <[>{b|B|l|L}<]> type 
Each field in the format specification may be a single character or a number which specifies a particular format option.

The type field is a single character that specifies whether the argument is interpreted as a character, string, number, or pointer, as shown in the following table.

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.
e float Floating-point number formatted as<[>-<]>d.dddde<[>-<]>dd.
E float Floating-point number formatted as<[>-<]>d.ddddE<[>-<]>dd.
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 (‘\0’).
p * A generic pointer formatted as t:aaaa where t is the memory type and aaaa is the hexadecimal address.

Note

The optional characters l or L may immediately precede the type character to respectively specify long types for d, i, u, o, x, and X. 
The optional characters b or B may immediately precede the type character to respectively specify char types for d, i, u, o, x, and X. 
Characters following a percent sign that are not recognized as a format specification are treated as ordinary characters. For example, “%%” writes a single percent sign to the output stream.

The flags field is a single character used to justify the output and to print +/- signs and blanks, decimal points, and octal and hexadecimal prefixes, as shown in the following table.

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.

The width field is a non-negative number that specifies the minimum number of characters printed. If the number of characters in the output value is less than width, blanks are added on the left (by default) or right (when the - flag is specified) to pad to the minimum width. If width is prefixed with a ‘0’, zeros are padded instead of blanks. The width field never truncates the output. If the length of the output value exceeds the specified width, all characters are output.

The width field may be an asterisk (‘*’), in which case an int argument from the argument list provides the width value. Specifying a ‘b’ in front of the asterisk specifies that the argument is an unsigned char.

The precision field is a non-negative number that specifies the number of characters to print, the number of significant digits, or the number of decimal places. The precision field can cause truncation or rounding of the output value in the case of a floating-point number as specified in the following table.

Type Precision Field Meaning 
d,u,o,x,X The 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. 
f The precision field specifies the number of digits to the right of the decimal point. The last digit is rounded. 
e,E The precision field specifies the number of digits to the right of the decimal point. The last digit is rounded. 
g,G The precision field specifies the maximum number of significant digits in the output value. 
s The precision field specifies the maximum number of characters in the output value. Excess characters are not output. 
c,p The precision field has no effect on these field types.

The precision field may be an asterisk (‘*’), in which case an int argument from the argument list provides the value. Specifying a ‘b’ in front of the asterisk specifies that the argument is an unsigned char.

Note

You must ensure that the argument type matches that of the format specification. You may use type casts to ensure that the proper type is passed to printf. 
This function is implementation-specific and is based on the operation of the _getkey and putchar functions. These functions, as provided in the standard library, read and write characters using the microcontroller’s serial port. Custom functions may use other I/O devices. 
The total number of bytes that may be passed to this function is limited due to the memory restrictions imposed by the 8051. A maximum of 15 bytes may be passed in SMALL or COMPACT model. A maximum of 40 bytes may be passed in LARGE model.

Return Value The printf function returns the number of characters actually written to the output stream.

See Also gets, printf517, puts, scanf, scanf517, sprintf, sprintf517, sscanf, sscanf517, vprintf, vsprintf

Example #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);
}

Keil C51 的printf的更多相关文章

  1. 【转】 Keil C51重定向printf到串口

    概述 进行C/C++开发的时候我们都会需要打印调试信息,打印调试信息时我们习惯使用printf函数,但是在Keil C51环境下,由于我们的程序是下载到单片机里,使用printf函数时不能直接打印到串 ...

  2. KEIL C51 printf格式化输出特殊用法

    作者:dragoniye   发布:2014-02-15 12:44   分类:硬件     抢沙发   /*******************************************KEI ...

  3. Keil C51软件的使用

    进入 Keil C51 后,屏幕如下图所示.几秒钟后出现编辑界 启动Keil C51时的屏幕 进入Keil C51后的编辑界面 简单程序的调试:学习程序设计语言.学习某种程序软件,最好的方法是直接操作 ...

  4. KEIL C51高级编程

    第一节 绝对地址访问C51提供了三种访问绝对地址的方法: 1. 绝对宏:在程序中,用“#include”即可使用其中定义的宏来访问绝对地址,包括:CBYTE.XBYTE.PWORD.DBYTE.CWO ...

  5. Keil C51编译及连接技术

    主要介绍Keil C51的预处理方法如宏定义.常用的预处理指令及文件包含指令,C51编译库的选择及代码优化原理,C51与汇编混合编程的方法与实现以及超过64KB空间的地址分页方法的C51实现. 教学目 ...

  6. keil C51 指针总结

    变量就是一种在程序执行过程中其值能不断变化的量.要在程序中使用变量必须先用标识符作为变量名,并指出所用的数据类型和存储模式,这样编译系统才能为变量分配相应的存储空间.定义一个变量的格式如下: [存储种 ...

  7. KEIL C51中const和code的使用

    code是KEIL C51 扩展的关键字,用code修饰的变量将会被放到CODE区里.但C语里的const关键字好像也有定义不能改变的变量的功能,这两个关键字有什么区别呢?在帮助手册里查找const, ...

  8. keil c51的内部RAM(idata)动态内存管理程序(转)

    源:keil c51的内部RAM(idata)动态内存管理程序 程序比较简单,但感觉比较有意思,个人认为有一定应用价值,希望大家有更好的思路和方法,互相促进. 程序的基本思路是:在CPU堆栈指针SP以 ...

  9. keil c51笔记

    第一章 Keil C51开发系统基本知识 第一节 系统概述 Keil C51是美国Keil Software公司出品的51系列兼容单片机C语言软件开发系统,与汇编相比,C语言在功能上.结构性.可读性. ...

随机推荐

  1. js 中实现aop

    http://fredrik.appelberg.me/2010/05/07/aop-js/ Aop = { // Apply around advice to all matching functi ...

  2. 20155308 加分题-mybash的实现(第五周)

    20155308 加分题-mybash的实现(第五周) 实验要求 使用fork,exec,wait实现mybash 写出伪代码,产品代码和测试代码 发表知识理解,实现过程和问题解决的博客(包含代码托管 ...

  3. 【BZOJ1041】[HAOI2008]圆上的整点

    [BZOJ1041][HAOI2008]圆上的整点 题面 bzoj 洛谷 题解 不妨设\(x>0,y>0\) \[ x^2+y^2=r^2\\ y^2=(x+r)(x-r) \] 设\(r ...

  4. 使用SDNN (space displacement neural network)进行多字体手写识别

    手写单字体的识别,在看过卷积神经网络的mnist例子之后,很容易实现,那么如何实现多字体的同时识别呢? 如下图 LeCun大神所用的是SDNN space displacement neural ne ...

  5. #6435. 「PKUSC2018」星际穿越

    考场上写出了70分,现在填个坑 比较好写的70分是这样的:(我考场上写的贼复杂) 设\(L(i)=\min_{j=i}^nl(j)\) 那么从i开始向左走第一步能到达的就是\([l(i),i-1]\) ...

  6. idea 和 WebStorm 配置 http代理 并更换主题

    proxy,http,socks5 当前 idea 主题为:(idea 自带) idea 编辑器的主题颜色字体为:(网上下载的 jar 包) 因为今天在安装下面这个主题时需要在 idea的 plugi ...

  7. Object C学习笔记3-对象的使用和定义

    1. 如何定义一个对象 在面向对象的语言中,定义一个对象是使用Class关键字,而在Object-C中则是使用@interface,@interface用于定义对象的属性和方法,@implementa ...

  8. layer.msg(msg, time, parme)设置图标问题

    layer.msg只提到3个参数,实际上有第4个参数,第4个参数就是msg关闭后执行的回调.   layer.msg('提示', 2, 1, function(){})   第一个参数:提示 第二个参 ...

  9. nginx 定义的一些状态码

    ngx_string(ngx_http_error_494_page), /* 494, request header too large */    ngx_string(ngx_http_erro ...

  10. TP里where的查询方式,比如or应该怎么写?

    这应该是个基础..只是我没有系统的学TP,所以用到了临时查了手册. 正常来说,thinkphp里的查询方式是: ThinkPHP可以支持直接使用字符串作为查询条件,但是大多数情况推荐使用数组或者对象来 ...