南昌中环地产总部电话:我想知道这个C语言程序的输出结果为什么是6。我看不懂这个程序,请大家帮忙解释一下。
来源:百度文库 编辑:中科新闻网 时间:2024/05/15 11:32:05
int f(char *s)
{ char *p=s;
while(*p!='\0')p++;
return(p-s);
}
void main()
{printf("%d\n",f("hello!"));
}
{ char *p=s;
while(*p!='\0')p++;
return(p-s);
}
void main()
{printf("%d\n",f("hello!"));
}
f的作用是计算字符串的长度,使用字符串尾地址减头地址实现,所以打印出来的就是hello!的长度6
因为6是hello!这个字符串的长度,如果你改成ABC,那结果就是3了.