有以下程序 #include #include main() { char p[20]={’a’, ’b’, ’c’, ’d’}, q[]="abc", r[]="abcde"; strcat(p, r);strcpy(p+strlen(q), q)

admin2015-07-31  19

问题 有以下程序
#include
#include
main()
{ char  p[20]={’a’, ’b’, ’c’, ’d’}, q[]="abc", r[]="abcde";
strcat(p, r);strcpy(p+strlen(q), q);
printf("%d\n", strlen(p));
}
程序运行后的输出结果是

选项 A、11
B、9
C、6
D、7

答案C

解析 strcpy:字符串拷贝函数;strlen:求字符串长度函数(注意:不包含字符串结束标记字符’\0’);strcat:字符串连接函数。执行完语句strcat(p,r);后,p数组中存储的元素为a,b,c,d,a,b,c,d,e ;执行语句strcpy(p+strlen(q), q); 得到的结果是将q所指向的字符串拷贝至p+strlen(q)开始的存储位置,因为strlen的值为3,即p+3开始存储q中的元素。所以执行完strcpy(p+strlen(q),q)语句后,字符数组p[20]的存储元素为a,b,c, a,b,c;所以strlen(p)的结果为6。因此C选项正确。
转载请注明原文地址:https://kaotiyun.com/show/CoDp777K
0

最新回复(0)