30.10.2014 Views

o_195h4n6al16jb186b1b2qs7fgssa.pdf

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

must be at least one named argument. The variable arguments are represented<br />

by<br />

ellipses (...). An object of type va_listmust be declared inside the function to<br />

hold<br />

the arguments. va_startwill initialize the variable to an argument list, va_argwill<br />

access the argument list, and va_endwill end the use of the argument.<br />

va_list<br />

Description: The type va_listdeclares a variable that will refer to each argument<br />

in a variable-length argument list.<br />

Include: <br />

Example: See va_arg.<br />

va_arg<br />

Description: Gets the current argument<br />

Include: <br />

Prototype: #define va_arg(va_list ap, Ty)<br />

Argument: ap pointer to list of arguments<br />

Ty type of argument to be retrieved<br />

Return Value: Returns the current argument<br />

Remarks: va_startmust be called before va_arg.<br />

Example: #include /* for printf */<br />

#include /*for va_arg, va_start,<br />

va_list, va_end */<br />

void tprint(const char *fmt, ...)<br />

{<br />

va_list ap;<br />

va_start(ap, fmt);<br />

while (*fmt)<br />

{<br />

switch (*fmt)<br />

{<br />

dsPIC<br />

®<br />

Language Tools Libraries<br />

Ví dụ: # include / * Cho offsetof * /<br />

# include / * Cho printf * /<br />

thông tin cấu trúc {<br />

char item1 [5];<br />

int Item2;<br />

char Item3;<br />

nổi item4;<br />

};<br />

int main (void)<br />

{<br />

printf ("Offset của item1 =% d \ n",<br />

offsetof (thông tin cấu trúc, item1));<br />

printf ("Offset của Item2 =% d \ n",<br />

offsetof (thông tin cấu trúc, Item2));<br />

printf ("Offset của Item3 =% d \ n",<br />

offsetof (thông tin cấu trúc, Item3));<br />

printf ("Offset của item4 =% d \ n",<br />

offsetof (thông tin cấu trúc, item4));<br />

}<br />

Đầu ra:<br />

Offset của item1 = 0<br />

Offset của Item2 = 6<br />

Offset của Item3 = 8<br />

Offset của item4 = 10<br />

Giải thích:<br />

Chương trình này cho thấy sự bù đắp bằng byte của mỗi thành viên trong cấu<br />

trúc từ<br />

bắt đầu của cấu trúc. Mặc dù item1is chỉ 5 byte (char<br />

item1 [5]), đệm được thêm vào để địa chỉ của item2falls trên<br />

thậm chí ranh giới. Điều tương tự cũng xảy ra với Item3; nó là 1 byte (char<br />

Item3) với 1 byte padding.<br />

Thư viện chuẩn C với hàm toán học<br />

2004 Microchip Technology Inc DS51456B trang 225<br />

DS51456B-page 222<br />

case '%':<br />

fmt++;<br />

if (*fmt == 'd')<br />

2004 Microchip Technology Inc.<br />

4.13 Vào và đầu ra<br />

Các stdio.hconsists tập tin tiêu đề của các loại, macro và các chức năng cung<br />

cấp hỗ trợ<br />

để thực hiện các hoạt động đầu vào và đầu ra trên các tập tin và suối. Khi một<br />

tập tin được mở ra nó là<br />

{<br />

int d = va_arg(ap, int);<br />

printf(" is an integer\n",d);<br />

kết hợp với một dòng suối. Một dòng là một đường ống dẫn cho các luồng dữ<br />

liệu vào và ra khỏi các tập tin.<br />

Bởi vì hệ thống khác nhau sử dụng các thuộc tính khác nhau, dòng cung cấp<br />

đồng đều hơn

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!