大宇私人领地伊甸园丨宁愿做了后悔,也不要错过后悔[www.mrdayu.com]
注册

24小时联系邮箱:[email protected]

所有文章
大宇私人伊甸园: 首页 > 所有文章 > 编程人生 > C/C++语言 > C标准库详解 > printf函数详解


友情链接:


printf函数详解

发布时间:2018/12/06 C标准库详解 标签printf阅读:25

函数原型:

int printf( const char *format, ... );​(until C99)
​int printf( const char *restrict format, ... );​(since C99)

头文件:

#include <stdio.h>

功能:

格式化字符串输出

说明:

format指定输出格式,后面跟要输出的变量
        目前printf支持以下格式:
          %c        单个字符
          %d        十进制整数
          %f        十进制浮点数
          %o        八进制数
          %s        字符串
          %u        无符号十进制数
          %x        十六进制数
          %%        输出百分号%
        一个格式说明可以带有几个修饰符,用来指定显示宽度,小数尾书及左对齐等:
          -         左对齐
          +         在一个带符号数前加"+"或"-"号
          0         域宽用前导零来填充,而不是用空白符
        域宽是一个整数,设置了打印一个格式化字符串的最小域。精度使用小数点后加数字表示的,
        给出每个转换说明符所要输出的字符个数。
     

特殊说明:

*     followed by integer number or *, or neither that specifies precision of the conversion. 
In the case when * is used, the precision is specified by an additional argument of type int. 
If the value of this argument is negative, it is ignored. If neither a number nor * is used, the precision is taken as zero. 
See the table below for exact effects of precision.
# :        alternative form of the conversion is performed. 
See the table below for exact effects otherwise the behavior is undefined.

Linux平台测试:

#include <stdio.h>

main()
{
        printf("Begin:\n");
        const char* s = "Hello";
        printf("\t.%10s.\n\t.%-10s.\n\t.%*s.\n\n\n", s, s, 10, s);
        printf("Sec Char:\n");
        printf("Characters:\t%c %%\n", 65);
        printf("Thri Integers:\n");
        printf("Decimal:\t%i %d %.6i %i %.0i %+i %i\n", 1, 2, 3, 0, 0, 4, -4);
        printf("Hexadecimal:\t%x %x %X %#x\n", 5, 10, 10, 7);
        printf("Octal:\t%o %#o %#o\n", 10, 10, 4);
        printf("\n\nFourth Floating point:\n");
        printf("Rounding:\t%f %.0f %.32f\n", 1.5, 1.5, 1.3);
        printf("Padding:\t%05.2f %.2f %5.2f\n", 1.5, 1.5, 1.5);
        printf("Scientific:\t%E %e\n", 1.5, 1.5);
        printf("Hexadecimal:\t%a %A\n", 1.5, 1.5);
}

运行:

[[email protected] STDC]$ gcc stdc_printf.c
[[email protected] STDC]$ ./a.out 
Begin:
	.     Hello.
	.Hello     .
	.     Hello.


Sec Char:
Characters:	A %
Thri Integers:
Decimal:	1 2 000003 0  +4 -4
Hexadecimal:	5 a A 0x7
Octal:	12 012 04


Fourth Floating point:
Rounding:	1.500000 2 1.30000000000000004440892098500626
Padding:	01.50 1.50  1.50
Scientific:	1.500000E+00 1.500000e+00
Hexadecimal:	0x1.8p+0 0X1.8P+0

Windows平台测试:

#include <stdio.h>

main()
{
	printf("Begin:\n");
	const char* s = "Hello";
	printf("\t.%10s.\n\t.%-10s.\n\t.%*s.\n\n\n", s, s, 10, s);
	printf("Sec Char:\n");
	printf("Characters:\t%c %%\n", 65);
	printf("Thri Integers:\n");
	printf("Decimal:\t%i %d %.6i %i %.0i %+i %i\n", 1, 2, 3, 0, 0, 4, -4);
	printf("Hexadecimal:\t%x %x %X %#x\n", 5, 10, 10, 7);
	printf("Octal:\t%o %#o %#o\n", 10, 10, 4);
	printf("\n\nFourth Floating point:\n");
	printf("Rounding:\t%f %.0f %.32f\n", 1.5, 1.5, 1.3);
	printf("Padding:\t%05.2f %.2f %5.2f\n", 1.5, 1.5, 1.5);
	printf("Scientific:\t%E %e\n", 1.5, 1.5);
	printf("Hexadecimal:\t%a %A\n", 1.5, 1.5);
	getchar();
}

运行结果:

Begin:
        .     Hello.
        .Hello     .
        .     Hello.


Sec Char:
Characters:     A %
Thri Integers:
Decimal:        1 2 000003 0  +4 -4
Hexadecimal:    5 a A 0x7
Octal:  12 012 04


Fourth Floating point:
Rounding:       1.500000 2 1.30000000000000000000000000000000
Padding:        01.50 1.50  1.50
Scientific:     1.500000E+000 1.500000e+000
Hexadecimal:    0x1.800000p+0 0X1.800000P+0

源码:

/* printf function */
#include "xstdio.h"

static void *prout(void *str, const char *buf, size_t n)
	{	/* write to file */
	return (fwrite(buf, 1, n, (FILE *)str) == n ? str : NULL);
	}

int (printf)(const char *fmt, ...)
	{	/* print formatted to stdout */
	int ans;
	va_list ap;

	va_start(ap, fmt);
	ans = _Printf(&prout, stdout, fmt, ap);
	va_end(ap);
	return (ans);
	}

va_list:

#ifdef _M_ALPHA
typedef struct {
        char *a0; /* pointer to first homed integer argument */
        int offset; /* byte offset of next parameter */
} va_list;
#else
typedef char * va_list;
#endif
The bottom line, for me, is simple.

Let’s hope it doesn’t take 500 years for a discipline of testing to becomes the standard for software developers.

From:dayu

您好!请登录

合作网站快捷登录:
点击取消回复

已有0评论

    大宇博客

    点击这里给我发消息 点击这里给我发消息 点击这里给我发消息 点击这里给我发消息