CODE
也就是說 要注意\0 的問題 不然會出錯
執行結果:
a1 + a2 = ABCDEFG|瓽篤HIJKLMN
a4 + a5 = ABCDEFGHIJKLMN
a6 + a7 = ABCDEFGHIJKLMN
size(a1) = 7 , size(a4)=8
CODE#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
char tmp[80];
char a1[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G'};
char a2[7] = {'H', 'I', 'J', 'K', 'L', 'M', 'N'};
char a4[8]="ABCDEFG";
char a5[8]="HIJKLMN";
char a6[8] = {'A', 'B', 'C', 'D', 'E', 'F', 'G','\0'};
char a7[8] = {'H', 'I', 'J', 'K', 'L', 'M', 'N','\0'};
sprintf(tmp, "%.7s%.7s", a1, a2);
printf("a1 + a2 = %s \n",tmp);
sprintf(tmp, "%s%s", a4, a5);
printf("a4 + a5 = %s \n",tmp);
sprintf(tmp, "%s%s", a6, a7);
printf("a6 + a7 = %s \n",tmp);
printf("size(a1) = %d , size(a4)=%d" , sizeof(a1),sizeof(a4));
system("PAUSE");
return 0;
}
也就是說 要注意\0 的問題 不然會出錯
執行結果:
a1 + a2 = ABCDEFG|瓽篤HIJKLMN
a4 + a5 = ABCDEFGHIJKLMN
a6 + a7 = ABCDEFGHIJKLMN
size(a1) = 7 , size(a4)=8
沒有留言:
張貼留言