C printf() 样例

本文详细介绍了C语言中printf函数的使用方法,包括基本语法、格式化选项及示例。讲解了整数、浮点数和字符串的格式化输出,并演示了宽度、精度控制等高级用法。

#include <stdio.h>
#include <stdlib.h>

int main( int argc, char* argv[] )
{
	printf("printf() Benchmark \n");
	printf("\n");

	printf("int printf(const char *format,[argument]);\n");
	printf("Print formatted data to stdout \n");
	printf("\n");

	printf("Format prototype: \n");
	printf("%%[flags][width][.precision][length]specifier \n");
	printf("\n");


	printf("Specifiers:\n");
	printf("d or i \t Signed decimal integer  \n");

	printf("u \t Unsigned decimal integer\n");

	printf("f \t Decimal floating point, lowercase	\n");
	printf("F \t Decimal floating point, uppercase \n");
	printf("e \t Scientific notation (mantissa/exponent), lowercase	\n");
	printf("E \t Scientific notation (mantissa/exponent) \n");
	printf("s \t String of characters \n");
	printf("\n");


	printf("The format specifier can also contain sub-specifiers: \n");
	printf("    flags, width, .precision and modifiers (in that order)\n");
	printf("\n");

	printf("width  \n");
	printf("(number)\t Minimum number of characters to be printed. \n");
	printf("        \t If the value to be printed is shorter than this number, \n");
	printf("        \t the result is padded with blank spaces. \n");
	printf("        \t The value is not truncated even if the result is larger. \n");
	printf("\n");


	printf(".precision  \n");
	printf(".number\t For integer specifiers (d, i, o, u, x, X):   \n");
	printf("       \t precision specifies the minimum number of digits to be written.\n");
	printf("       \t If the value to be written is shorter than this number, \n");
	printf("       \t the result is padded with leading zeros. \n");
	printf("       \t The value is not truncated even if the result is longer. \n");
	printf("       \t A precision of 0 means that no character is written for the value 0. \n");
	printf("       \t For a, A, e, E, f and F specifiers: \n");
	printf("       \t this is the number of digits to be printed after the decimal point \n");
	printf("       \t (by default, this is 6). \n");


	printf("\n");
	printf("Characters: %c %c \n", 'a', 65);
	printf("Decimals: %d %ld\n", 1977, 650000L);

	printf("Some different radices: %d %x %o %#x %#o \n", 100, 100, 100, 100,100);
	printf("floats: %4.2f %+.0e %E \n", 3.1416, 3.1416, 3.1416);
	printf("Width trick: %*d \n", 5, 10);
	printf("\n");

	double value = 123456.123456;

	printf("Testing value = %f \n", value);

	printf("printf(\"%%f\"):%f \n", value);
	printf("printf(\"%%6.3f\"):%6.3f \n", value);
	printf("printf(\"%%6.6f\"):%6.6f \n", value);
	printf("printf(\"%%6.8f\"):%6.8f \n", value);
	printf("printf(\"%%16.8f\"):%6.8f \n", value);

	printf("printf(\"%%e\"):%E\n", value);
	printf("\n");
	printf("Preceding with blanks:  \n");
	printf("printf(\"%%10d\"):%10d\n", 1977);

	printf("\n");
	printf("Preceding with zeros:  \n");
	printf("printf(\"%%010d\"):%010d \n", 1977);

	printf("\n");
	printf("%s \n", "String Benchmarks");
	printf("printf(\"%%s\"):%s\n", "Hello, world!");
	printf("printf(\"%%15s\"):%15s\n", "Hello, world!");
	printf("printf(\"%%.10s\"):%.10s\n", "Hello, world!");
	printf("printf(\"%%-10s\"):%-10s\n", "Hello, world!");
	printf("printf(\"%%-15s\"):%-15s\n", "Hello, world!");
	printf("printf(\"%%.15s\"):%.15s\n", "Hello, world!");
	printf("printf(\"%%15.10s\"):%15.10s\n", "Hello, world!");
	printf("printf(\"%%-15.10s\"):%-15.10s\n", "Hello, world!");

	return 0;
}

输出结果:

printf() Benchmark

int printf(const char *format,[argument]);
Print formatted data to stdout

Format prototype:
%[flags][width][.precision][length]specifier

Specifiers:
d or i   Signed decimal integer
u        Unsigned decimal integer
f        Decimal floating point, lowercase
F        Decimal floating point, uppercase
e        Scientific notation (mantissa/exponent), lowercase
E        Scientific notation (mantissa/exponent)
s        String of characters

The format specifier can also contain sub-specifiers:
    flags, width, .precision and modifiers (in that order)

width
(number)         Minimum number of characters to be printed.
                 If the value to be printed is shorter than this number,
                 the result is padded with blank spaces.
                 The value is not truncated even if the result is larger.

.precision
.number  For integer specifiers (d, i, o, u, x, X):
         precision specifies the minimum number of digits to be written.
         If the value to be written is shorter than this number,
         the result is padded with leading zeros.
         The value is not truncated even if the result is longer.
         A precision of 0 means that no character is written for the value 0.
         For a, A, e, E, f and F specifiers:
         this is the number of digits to be printed after the decimal point
         (by default, this is 6).

Characters: a A
Decimals: 1977 650000
Some different radices: 100 64 144 0x64 0144
floats: 3.14 +3e+00 3.141600E+00
Width trick:    10

Testing value = 123456.123456
printf("%f"):123456.123456
printf("%6.3f"):123456.123
printf("%6.6f"):123456.123456
printf("%6.8f"):123456.12345600
printf("%16.8f"):123456.12345600
printf("%e"):1.234561E+05

Preceding with blanks:
printf("%10d"):      1977

Preceding with zeros:
printf("%010d"):0000001977

String Benchmarks
printf("%s"):Hello, world!
printf("%15s"):  Hello, world!
printf("%.10s"):Hello, wor
printf("%-10s"):Hello, world!
printf("%-15s"):Hello, world!
printf("%.15s"):Hello, world!
printf("%15.10s"):     Hello, wor
printf("%-15.10s"):Hello, wor

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值