2013年6月11日 星期二

DIY - 智能小車:CCS C 語言的 sprintf 指令 (三十五) :

DIY - 智能小車:CCS C 語言的 sprintf 指令 ()

CCS C 語言除 printf fprint 指令外,還有 sprintf 指令,基本格式(Format)是一樣,主要是輸出字符串將用 null\0)作終止,有時需要終結用 null\0)便不用自己加 null 了。 

句法(Syntax):
sprintf(string, cstring, values...)  

參數(Parameters):
字符串是一個字符矩陣。
CString是一個常量字符串或終止空字符矩陣。數值是一個用逗號分隔的變量列表。

返回(Returns):


功能(Function):
此功能操作如 printf,但其輸出被放置到指定的字符串。輸出字符串將用 null 作終止。不進行任何檢查,確保該字符串是足夠大的數據。

格式(Format):
類型
描述
c
Character
s
String or character
u
Unsigned int
d
Singed int
Lu
Long unsigned int
Ld
Long signed int
x
Hex int (Lower case)
X
Hex int (Upper case)
Lx
Hex long int (Lower case)
LX
Hex long int (Upper case)
f
Float with truncated decimal
g
Float with rounded decimal
e
Float in exponential format
w
Unsigned int with decimal place inserted. Specify two numbers for n. The first is a total field width. The second is the desired number of decimal places.

格式示例:
Specifier
Value=0x12
Value=0xfe
%03u
018
254
%u
18
254
U%2u
18
*
%5
18
254
%d
18
-2
%x
12
fe
%X
12
FE
%4X
0012
00FE
%3.1w
1.8
25.4

sprintf 程式:
char mystring[20];
long mylong;
mylong=1234;
sprintf(mystring,"<%lu>",mylong);
// mystring now has:
// < 1 2 3 4 > 0

2013611 天氣報告
氣溫:24.1 @ 22:20
相對濕度:百分之91%
天氣:微雨

沒有留言:

張貼留言