问题描述
在我测试自己的课设时,遇到了学期的格式问题:
这是一部分的检查格式的代码:
char t[2];
t[0] = date[5];
t[1] = date[6];
if (strcmp(t, "春") != 0 && strcmp(t, "夏") != 0 && strcmp(t, "秋") != 0) {
printf("格式错误,请重新输入:n");
return false;
}
原因分析:
我感觉应该是没有在t的末尾加上 ‘ ’ 的原因,加上后果然就没问题了。
在此之前我写了段测试代码在devc++和vscode中分别测试了一下
#include#include #include int main(){ char date[10]="2022-春"; printf("%dn",strlen(date)); char t[2]; t[0] = date[5]; t[1] = date[6]; printf("%sn",t); printf("%dn",strcmp(t,"春")); system("pause"); return 0; }
这是devc++的结果:
这是vscode的结果:
两个都奇奇怪怪的。一个认为t和“春”相等,一个认为t指向的字符串是“春2022-春”。
不可思议。我保证代码是一样的,复制粘贴过去的。
有没有谁可以解答一下这个现象呢?



