uTools-Manuals/docs/c/printf.html
2019-04-21 11:50:48 +08:00

85 lines
2.7 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<h1>输入输出 - printf</h1>
<p>原型extern void printf(const char *format,...);</p>
<p>用法:#include &lt;stdio.h></p>
<p>功能:格式化字符串输出</p>
<p>说明format指定输出格式后面跟要输出的变量</p>
<pre><code class="language-c">
目前printf支持以下格式
%c 单个字符
%d 十进制整数
%f 十进制浮点数
%o 八进制数
%s 字符串
%u 无符号十进制数
%x 十六进制数
%% 输出百分号%
一个格式说明可以带有几个修饰符,用来指定显示宽度,小数尾书及左对齐等:
- 左对齐
+ 在一个带符号数前加"+"或"-"号
0 域宽用前导零来填充,而不是用空白符
域宽是一个整数,设置了打印一个格式化字符串的最小域。精度使用小数点后加数字表示的,
给出每个转换说明符所要输出的字符个数。
<font color=red>注意</font>:带修饰符的显示可能不正常
</code></pre>
举例:<pre><code class="language-c">
// printf.c
#include &lt;stdio.h>
#include &lt;system.h>
main()
{
int i;
char *str="GGV";
clrscr();
textmode(0x00);
printf("Printf Demo-%%c");
printf("--------------");
printf("%c-%c-%c-%c\n",'D','e','m','o');
printf("%2c-%2c-%2c-%2c\n",'D','e','m','o');
printf("%02c-%02c-%02c-%02c\n",'D','e','m','o');
printf("%-2c-%-2c-%-2c-%-2c\n",'D','e','m','o');
getchar();
clrscr();
textmode(0x00); // not nessary
i=7412;
printf("Printf Demo-%%d");
printf("--------------");
printf("%d\n",i);
printf("%14d",i);
printf("%+10d\n",i); // output format not correct(bug)
printf("%-10d\n",i);
getchar();
clrscr();
printf("Printf - d,o,x");
printf("--------------");
printf("%d\n",i);
printf("%o\n",i); // %o and %x not implemented
printf("%x\n",i);
getchar();
clrscr();
printf("Printf Demo-%%s");
printf("--------------");
printf(" %s\n","Demo End");
printf(" %s\n","Thanx");
printf(" %s\n %s","Golden","Global View");
getchar();
return 0;
}
</code></pre>相关函数:无