帮我解释一下这个C程问题

来源:百度知道 编辑:UC知道 时间:2024/05/26 05:36:09
#include<stdio.h>
main()
{
char *p,s[256];
p=s;
while(strcmp(s,"the end"))
{
printf("Input the string: ");
gets(s);
while(*p)
putchar(*p++);
}
}

这个程序为什么直到接收到字符串 the end 才结束 , 输入的字符串小于 the end 为什么不能让程序结束 strcmp是比较两个字符串大小的 那两个字符串相等了怎么办

strcmp是比较两个字符串大小的 那两个字符串相等了返回值是0,这样循环条件就不成立了,结束程序.
int strcmp(char *s1,char *s2);
如果s1>S2返回1,相等返回0,小于返回-1,
比较的是字符的ascii值.

我想应该是这样的,只有为0时才为C语言才认为假,其它都作为真处理,小于0也为真,如果改为
while(strcmp(s,"the end")<=0) 应该就会结束了

int strcmp(char *s1,char *s2);
如果s1>S2返回1,相等返回0,小于返回-1,

只有等于0的时候才认为false。
-1和1都认为是true

郁闷!你应该先输入字符串S的值吧!while函数可和do while不一样,你再执行while循环的时候先gets(s);然后再看看结果吧!

strcpy