现在的位置: 首页 > 综合 > 正文

[编程语言]C格式化输出实例

2013年01月23日 ⁄ 综合 ⁄ 共 1509字 ⁄ 字号 评论关闭
int a = 4294965296;
char *str = "a STr";
printf("0x%08x\n", a);		//输出8位16进制数,不足补零,结果: 0xfffff830
printf("0x%p\n", str);		//输出指针地址				 结果:	0x004156BC

字符

输入数据类型

含义

d、i

int

有符号10进制数,i是老式写法

o

unsigned int

无符号8进制数

u

unsigned int

无符号10进制数

x、X

unsigned int

无符号16进制数,x用abcdef,X用ABCDEF表示10~15的数

f

double

小数

e、E

double

科学计数法表示的数,大小写代表用的“e”的大小写

g、G

double

使用以上两种中最短的形式,大小写的使用同%e和%E

c

char

把输入的数字转换为对应的字符

s、S

char *、wchar_t *

字符串

p

void *

以16进制形式输出指针

n

int *

到此字符之前为止,一共输出的字符个数,不输出文本

%

不输入

输出字符“%”本身

附:MFC中的CTime格式化输出

	CTime time = CTime::GetCurrentTime(); 	
	CString msgContent = time.Format("Current Date is: %x(%Y-%b(%m)-%d), %U weeks of year, %A, %H:%M:%S, %I");

输出结果:

Current Date is: 11/02/13(2013-Nov(11)-02), 43 weeks of year, Saturday, 00:48:53, 12

http://msdn.microsoft.com/en-us/library/vstudio/fe06s4ak.aspx

%a
Abbreviated weekday name

%A
Full weekday name

%b
Abbreviated month name

%B
Full month name

%c
Date and time representation appropriate for locale

%d
Day of month as decimal number (01 – 31)

%H
Hour in 24-hour format (00 – 23)

%I
Hour in 12-hour format (01 – 12)

%j
Day of year as decimal number (001 – 366)

%m
Month as decimal number (01 – 12)

%M
Minute as decimal number (00 – 59)

%p
Current locale's A.M./P.M. indicator for 12-hour clock

%S
Second as decimal number (00 – 59)

%U
Week of year as decimal number, with Sunday as first day of week (00 – 53)

%w
Weekday as decimal number (0 – 6; Sunday is 0)

%W
Week of year as decimal number, with Monday as first day of week (00 – 53)

%x
Date representation for current locale

%X
Time representation for current locale

%y
Year without century, as decimal number (00 – 99)

%Y
Year with century, as decimal number

%z, %Z
Either the time-zone name or time zone abbreviation, depending on registry settings; no characters if time zone is unknown

抱歉!评论已关闭.