引起一些问题 [英] strlen causing some issues
问题描述
由于某种原因,当我尝试编译该程序时,发生了奇怪的事情,例如文件名更改为"string.h".有任何想法吗?
For some reason when I try to compile this program strange things are happening like the file name is changed to "string.h". Any ideas?
#include <stdio.h>
#include <string.h>
int main()
{
char str1[20] = "Hello\0" ;
char str2[20] = "everyone\0" ;
printf("%i\n",strlen(str1)) ;
printf("%i\n",strcmp(str2)) ;
return 0;
}
推荐答案
标准的strcmp()函数使用两个参数,因为它比较了两个字符串.我很惊讶您的编译器甚至可以在其中编译带有"strcmp(str2)"的行,这真的不应该.
The standard strcmp() function takes two arguments, since it compares two strings. I''m surprised your compiler will even compile that line with "strcmp(str2)" in it -- it really shouldn''t.
顺便说一句,您不必在定义的字符串的末尾添加"\ 0".
By the way, you don''t have to add "\0" to the ends of the strings you define. The compiler will do that for you.
我认为strcmp应该比较2个字符串.
I thought strcmp was supposed to compare 2 strings.
哪个文件正在更改为string.h?上面的代码所在的文件?
And which file is changing to string.h? The file that the above code is in?
这篇关于引起一些问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!