问题遇到的现象和发生背景
首先,我输入hello的话,取的strlen值应该是6,然后我赋值给len,则len=6,然后我让c[len+1]赋值'\0',按理说应该是c[7]='\0',结果输出确实对的,这是为什么啊,用的vs2022
问题相关代码,请勿粘贴截图
#include <stdio.h>
#include <string.h>
int main()
{
char c[100],b[100];
gets(c);
int len = strlen(c);
c[len+1] = '\0';
int i, j;
for (i = strlen(c)-1,j=0;i >= 0;i--,j++)
{
b[j] = c[i];
}
b[j] = '\0';
int result = strcmp(c, b);
if (result < 0) {
printf("%d\n", -1);
}
else if (result > 0)
{
printf("%d\n", 1);
}
else {
printf("%d\n", 0);
}
return 0;
}
运行结果及报错内容
hello
-1