不知为什么不相等解决方案

不知为什么不相等
#include <stdio.h>
#include <stdlib.h>
main()
{
                char   buf[50];
                int   num;
                buf1=fgets(buf,50,stdin);   //stdin为标准输入
        printf( "%s ",buf1);       //从键盘输入buf1为xyz          
              if(strcmp(buf1, "xyz ")==0)
                    printf( "is   xyz ");
              else
                  printf( "is   not   xyz ");
         
}
可得到的buf1和“xyz”不相等。请朋友们给予指点!


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char buf[50];
char *buf1;

buf1=fgets(buf,50,stdin); //stdin为标准输入
printf( "%s ",buf1); //从键盘输入buf1为xyz
if(strcmp(buf1, "xyz\n ")==0)
printf( "is xyz\n ");
else
printf( "is not xyz\n ");
return 0;
}
fgets reads characters from the current stream position to and including the first newline character, to the end of the stream, or until the number of characters read is equal to n–1.


楼上正解: fgets会包括尾部的一个\n ,并在尾部添加一个\0…


right

不知为什么不相等解决方案

相关文章:

你感兴趣的文章:

标签云: