Keil C51 的printf sprintf

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

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

<code class="hljs cpp has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">char</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">putchar</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">char</span> c)   
{        
    ES=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>;        
    SBUF = c;        
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">while</span>(TI==<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>);        
    TI=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>;        
    ES=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>;        
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>;
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li></ul>

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

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

<code class="hljs cpp has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> main()
{
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">unsigned</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">char</span> test1 = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">55</span>;
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">printf</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"the test is %d\r\n"</span>,test1);
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>

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

<code class="hljs cpp has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> main()
{
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">unsigned</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">char</span> test1 = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">55</span>;
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">printf</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"the test is %d\r\n"</span>,(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>)test1);
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>

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

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

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

<code class="hljs cpp has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> main()
{
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">unsigned</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">char</span> test1 = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">55</span>;
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">printf</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"the test is %bd\r\n"</span>,test1);
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>

下面附上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

<code class="hljs perl has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">Example <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#include <stdio.h></span>

void tst_printf (void) {
  char a = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>;
  <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> b  = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12365</span>;
  long c = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x7FFFFFFF</span>;

  unsigned char <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">x</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'A'</span>;
  unsigned <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">y</span>  = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">54321</span>;
  unsigned long z = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x4A6F6E00</span>;

  float f = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10.0</span>;
  float g = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">22.95</span>;

  char buf [] = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Test String"</span>;
  char <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">*p</span> = buf;

  <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">printf</span> (<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"char <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%bd</span> int <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%d</span> long <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%ld</span>\n"</span>,a,b,c);
  <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">printf</span> (<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Uchar <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%bu</span> Uint <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%u</span> Ulong <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%lu</span>\n"</span>,<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">x</span>,<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">y</span>,z);
  <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">printf</span> (<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"xchar <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%bx</span> xint <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%x</span> xlong <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%lx</span>\n"</span>,<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">x</span>,<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">y</span>,z);
  <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">printf</span> (<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"String <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%s</span> is at address <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%p</span>\n"</span>,buf,p);
  <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">printf</span> (<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%f</span> != <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%g</span>\n"</span>, f, g);
  <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">printf</span> (<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%*</span>f != <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%*</span>g\n"</span>, (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>)<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</span>, f, (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>)<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</span>, g);
}</code>

printf

Summary
#include <stdio.h>

int printf (
  const char *<em>fmtstr</em>       /* format string */
  <[>, <em>arguments</em> ... <]>);   /* 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 theputcharfunction. 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 additionalarguments are included in the printf function call.

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

Format specifications have the following general format:

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

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.

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.
EfloatFloating-point number formatted as
<[>-<]>d.ddddE<[>-<]>dd.
gfloatFloating-point number using either the e or f format, whichever is more compact for the specified value and precision.
GfloatFloating-point number using either the E or f format, whichever is more compact for the specified value and precision.
ccharA 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 andaaaa 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.

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, 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. Theprecision field can cause truncation or rounding of the output value in the case of a floating-point number as specified in the following table.

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

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

C51: PRINTF GIVES WRONG VALUES


Information in this article applies to:

  • C51 All Versions

QUESTION

In C51, I am using printf and sprintf with many arguments and it is printing out wrong values. What is the reason for this behavior?

ANSWER

There are any number of reasons why printf can output incorrect values.

Probably the most common reason is that the format specifier and the argument type do not match. To solve this kind of problem, explicitly type cast each argument to the desired type. Be sure that you accurately specify the correct format specifier for that type.

Another reason may be that the number of bytes you may pass to functions with variable-length argument lists is fixed (since arguments are not passed on the stack). In small and compact memory model, printf and sprintf only reserve 15 bytes for the argument list. In the large memory model 40 bytes are reserved. For example, 15 bytes allow you to pass up to five generic (3-byte) pointers, or one generic pointer and three long arguments. This parameter limitation is a compromise to the limited memory available in 8051 devices.

Eight arguments will not fit in the space reserved for small or compact memory models. So, instead of one sprintf call with many arguments you must perform two or three calls and distribute the arguments among the sprintf calls. For example:

The following code exceeds the parameter space limits since seven generic pointers (21 bytes) are passed to sprintf.

char str[] = "Any Text";
sprintf (buff_out, "%s %s %s %s %s", str, str, str, str, str);

The above example may be rewritten as follows:

char str[] = "Any Text";
int len;

len = sprintf (buff_out, "%s %s %s ", str, str, str);
sprintf (buff_out + len, "%s %s", str, str);

MORE INFORMATION

  • Refer to printf in the Cx51 User's Guide.

printf 打印signed char 与unsigned char 之区别 

先看一下例子

#include

int main()
{
int i;
unsigned char *p;
char *p1;
int a[] = {0xffffffff, 0xffffffff, 0xffffffff};
p = a;
p1 = a;
for(i = 0 ; i < 8 ; i++) {
printf(" 0x%02x  0x%02x \n", p[i], p1[i]);
}
}
$ gcc main.c 
main.c: In function ‘main’:
main.c:10: warning: assignment from incompatible pointer type
main.c:11: warning: assignment from incompatible pointer type
$ ./a.out 
 0xff  0xffffffff 
 0xff  0xffffffff 
 0xff  0xffffffff 
 0xff  0xffffffff 
 0xff  0xffffffff 
 0xff  0xffffffff 
 0xff  0xffffffff 
 0xff  0xffffffff 
看来是在printf的参数传递过程中出现了问题,%x传递的参数的大小是四个字节,反汇编看一下
¥gcc -S main.c -o main.s
$ vi main.s
.file "main.c"
.section .rodata
.LC0:
.string " 0x%02x  0x%02x \n"
.text
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
andl $-16, %esp
subl $48, %esp
movl $-1, 24(%esp)
movl $-1, 28(%esp)
movl $-1, 32(%esp)
leal 24(%esp), %eax
movl %eax, 40(%esp)
leal 24(%esp), %eax
movl %eax, 44(%esp)
movl $0, 36(%esp)
jmp .L2
.L3:
movl 36(%esp), %eax
addl 44(%esp), %eax
movzbl (%eax), %eax
movsbl %al, %ecx
movl 36(%esp), %eax
addl 40(%esp), %eax
movzbl (%eax), %eax
movzbl %al, %edx
movl $.LC0, %eax
movl %ecx, 8(%esp)
movl %edx, 4(%esp)
movl %eax, (%esp)
call printf
addl $1, 36(%esp)
.L2:
cmpl $7, 36(%esp)
jle .L3
leave
ret
.size main, .-main
.ident "GCC: (GNU) 4.4.5 20101112 (Red Hat 4.4.5-2)"
.section .note.GNU-stack,"",@progbits

可见,在printf执行的时候传递的两个参数分别使用了 movsbl 和movzbl,这两个指令有什么差别呢

movsbl、movzbl的区别:
    %dh = 8D
    %eax = 98765432

    movb %dh,%al    //%eax = 9876548D

    movsbl %dh,%eax    //%eax = FFFFFF8D
    movzbl %dh,%eax    //%eax = 0000008D

如果 我们要一个字节一个字节的打印内存的信息的时候,还是需要使用unsigned char 作为指针进行打印比较好。

看一下mips 反汇编的结果
       lw      $2,24($fp)
        slt     $2,$2,8
        beq     $2,$0,$L3
        lw      $3,28($fp)
        lw      $2,24($fp)
        addu    $2,$3,$2
         lbu     $5,0($2)
        lw      $3,32($fp)
        lw      $2,24($fp)
        addu    $2,$3,$2
         lb      $2,0($2)
        la      $4,$LC1
        move    $6,$2
        jal     printf
        lw      $2,24($fp)
        addiu   $2,$2,1
        sw      $2,24($fp)
        b       $L2
看一下arm 平台下运行的结果
# ./a.out 
 0xff  0xff 
 0xff  0xff 
 0xff  0xff 
 0xff  0xff 
 0xff  0xff 
 0xff  0xff 
 0xff  0xff 
 0xff  0xff 
# cat /proc/cpuinfo 
Processor       : ARM926EJ-S rev 5 (v5l)
BogoMIPS        : 99.12
Features        : swp half thumb fastmult edsp java 
CPU implementer : 0x41
CPU architecture: 5TEJ
CPU variant     : 0x0
CPU part        : 0x926
CPU revision    : 5

Hardware        : Atmel AT91SAM9260-EK
Revision        : 0000
Serial          : 0000000000000000

再看一下arm 的反汇编
.L3:
        ldr     r3, [fp, #-16]
        mov     r2, r3
        ldr     r3, [fp, #-12]
        add     r3, r2, r3
         ldrb    r3, [r3, #0]    @ zero_extendqisi2
        mov     r1, r3
         ldr     r3, [fp, #-16]
        mov     r2, r3
        ldr     r3, [fp, #-8]
        add     r3, r2, r3
         ldrb    r3, [r3, #0]    @ zero_extendqisi2
        ldr     r0, .L6+4
        mov     r2, r3
        bl      printf
        ldr     r3, [fp, #-16]
        add     r3, r3, #1
        str     r3, [fp, #-16]
arm 平台跟 mips 和 x86 有比较大的差别
LDRB     指令从value 与basereg 将一个字节加载到dest

当然,正如网友说的,
根本原因如下:
%x是无符号整数的16进制,是无符号!而你的printf传递的实际是字符型,于是有一个类型提升的问题。p是无符号数,那么0xff提升为无符号整数仍然是0xff,而p1是有符号的,0xff实际的值是-1.那么对应的无符号整数仍然应该为-1的对应值,也就是0xffffffff。这才是根本原因。
其实对于这个问题根本不需要使用反汇编。C语言基础就解决了!

printf 要打印一个%x的话,要传递一个4字节的数据,而对于singed 和unsigned char 的处理来说将一个char 转化成一个4字节的数据 在x86 平台上有 movsbl、movzbl  区别的对待有符号和无符号的数据,但是对于arm 来说,编译器通过Ldr只把一个字节的数据传递到参数里面就行,这个是由于编译器的不同而不同的。我只是想看一下,对于有符号和无符号的数据处理,编译器到底是怎么处理的?为什么要照搬教条性的东西呢?如果按照教条性的东西,为什么有的平台打印会不同呢?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值