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.

else if (res == 0)<br />

printf("20 characters of buf1 and buf3 "<br />

"are equal\n");<br />

else<br />

printf("buf3 comes before buf1\n");<br />

}<br />

Output:<br />

buf1 : Where is the time?<br />

buf2 : Where did they go?<br />

buf3 : Why?<br />

6 characters of buf1and buf2 are equal<br />

buf2 comes before buf1<br />

buf1 comes before buf3<br />

memcmp (Continued)<br />

Standard C Libraries with Math Functions<br />

2004 Microchip Technology Inc. DS51456B-page 297<br />

memcpy<br />

Description: Copies characters from one buffer to another.<br />

Include: <br />

Prototype: void *memcpy(void *dst, const void *src, size_t n);<br />

Arguments: dst buffer to copy characters to<br />

src buffer to copy characters from<br />

n number of characters to copy<br />

Return Value: Returns dst.<br />

Remarks: memcpycopies ncharacters from the source buffer srcto the<br />

destination buffer dst. If the buffers overlap, the behavior is undefined.<br />

Example: #include /* memcpy */<br />

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

int main(void)<br />

{<br />

char buf1[50] = "";<br />

char buf2[50] = "Where is the time?";<br />

char buf3[50] = "Why?";<br />

printf("buf1 : %s\n", buf1);<br />

printf("buf2 : %s\n", buf2);<br />

printf("buf3 : %s\n\n", buf3);<br />

memcpy(buf1, buf2, 6);<br />

printf("buf1 after memcpy of 6 chars of "<br />

"buf2: \n\t%s\n", buf1);<br />

Mô tả: Gắn thêm một bản sao của mã nguồn stringto cuối của chuỗi đích.<br />

Bao gồm:<br />

Prototype: char * strcat (char * s1, const char * s2);<br />

Đối số: s1 null chấm dứt chuỗi đích để sao chép vào<br />

s2 null chấm dứt chuỗi nguồn được sao chép<br />

Quay trở lại giá trị: Trả về một con trỏ đến chuỗi đích.<br />

Ghi chú: Chức năng này gắn thêm chuỗi nguồn (bao gồm cả việc chấm dứt vô<br />

nhân vật) vào cuối của chuỗi đích. Các nhân vật ban đầu của<br />

chuỗi nguồn sẽ ghi đè lên các ký tự null ở cuối của<br />

chuỗi đích. Nếu bộ đệm chồng lên nhau, hành vi này là không xác định.<br />

Ví dụ: # include / * Cho strcat, strlen * /<br />

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

int main (void)<br />

{<br />

char buf1 [50] = "Chúng tôi ở đây";<br />

char buf2 [50] = "đâu là thời điểm";<br />

printf ("buf1:% s \ n", buf1);<br />

printf ("\ t (% d ký tự) \ n \ n", strlen (buf1));<br />

printf ("buf2:% s \ n", buf2);<br />

printf ("\ t (% d ký tự) \ n \ n", strlen (buf2));<br />

strcat (buf1, buf2);<br />

printf ("buf1 sau strcat của buf2: \ n \ t% s \ n",<br />

buf1);<br />

printf ("\ t (% d ký tự) \ n", strlen (buf1));<br />

printf ("\ n");<br />

strcat (buf1, "Tại sao?");<br />

printf ("buf1 sau strcatof \" Tại sao \? ": \ n \ t% s \ n",<br />

buf1);<br />

printf ("\ t (% d ký tự) \ n", strlen (buf1));<br />

}<br />

Đầu ra:<br />

buf1: Chúng tôi ở đây<br />

(10 ký tự)<br />

buf2: đâu là thời gian?<br />

(18 ký tự)<br />

buf1 sau strcat của buf2:<br />

Chúng tôi hereWhere là thời gian?<br />

(28 ký tự)<br />

buf1 sau strcat của "Tại sao?":

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

Saved successfully!

Ooh no, something went wrong!