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.

(string length = 11 characters)<br />

str2 :<br />

(string length = 0 characters)<br />

str3 : Why me?<br />

(string length = 7 characters)<br />

strncat<br />

Description: Append a specified number of characters from the source string to<br />

the<br />

destination string.<br />

Include: <br />

Prototype: char *strncat(char *s1, const char *s2, size_t n);<br />

Arguments: s1 destination string to copy to<br />

s2 source string to copy from<br />

n number of characters to append<br />

Return Value: Returns a pointer to the destination string.<br />

Remarks: This function appends up to ncharacters (a null character and<br />

characters that follow it are not appended) from the source string to the<br />

end of the destination string. If a null character is not encountered, then<br />

a terminating null character is appended to the result. If the strings<br />

overlap, the behavior is undefined.<br />

Example: #include /* for strncat, strlen */<br />

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

int main(void)<br />

{<br />

char buf1[50] = "We're here";<br />

Đối số: s1 chuỗi đích để sao chép vào<br />

chuỗi nguồn s2 sao chép từ<br />

số n ký tự sao chép<br />

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

Bình luận: Bản sao ncharacters từ chuỗi nguồn vào chuỗi đích. Nếu<br />

chuỗi nguồn ít hơn ncharacters, điểm đến đầy<br />

ký tự null tổng ncharacters. Nếu ncharacters đã được sao chép và không có<br />

ký tự rỗng được tìm thấy sau đó các chuỗi điểm đến sẽ không được<br />

null-chấm dứt. Nếu chuỗi chồng chéo lên nhau, hành vi này là không xác định.<br />

Ví dụ: # include / * Forstrncpy, 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 />

char buf3 [50] = "Tại sao?";<br />

buf4 char [7] = "Ở đâu?";<br />

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

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

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

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

strncmp (Tiếp theo)<br />

dsPIC<br />

®<br />

Thư viện Công cụ Ngôn ngữ<br />

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

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

DS51456B trang 310<br />

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

2004 Microchip Technology Inc<br />

strlen (Continued)<br />

Standard C Libraries with Math Functions<br />

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

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

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

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

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

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

printf("\t(%d characters)\n\n\n", strlen(buf3));<br />

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

printf("buf1 after strncat of 6 characters "<br />

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

printf ("buf1 sau strncpy 6 ký tự"<br />

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

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

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

strncpy (buf1, buf2, 18);<br />

printf ("buf1 sau strncpy của 18 nhân vật"<br />

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

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

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

strncpy (buf1, buf3, 5);<br />

printf ("buf1 sau strncpy 5 ký tự"<br />

"Của buf3: \ n \ t% s \ n", buf1);

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

Saved successfully!

Ooh no, something went wrong!