繁体   English   中英

Integer:为什么它在 Linux 上工作而不在 Windows 上工作

[英]Integer: Why does it work on Linux and not on Windows

我有一个大问题。 我是 C 的初学者,我的任务是编写一个在我的房间(理论上)盘旋的“机器人”。 我用这个方法试过了:index = 0; 如果你输入“是”,它会在索引上加 1,如果你回答“否”或其他东西,它只会打印一些东西。 如果 index >= 4 它将打印一些东西并停止程序。

我用 cc 在我的 Raspberry PI 4 上编译它并且它完全可以正常工作但是如果我使用代码块(我认为它使用 GCC)它会将 Integer 设置为 115(如果我输入是,否则它将为 0)并添加1 到 116,我完全不知道为什么。 但我认为这是编译器的问题。 如何解决这个问题?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main()
{
char wall[2];
int index;
index = 0;
 while(wall[0] == '\0'){ // Check if wall[0] is empty

        printf("Go!\n");
        printf("Is the Wall already there?\n");
        printf("Number: %i\n", index);
        scanf("%s", wall);

      if(strcmp(wall, "yes") == 0){
          printf("Turn!\n");
          index++;
          if(index >= 4){
              printf("You have circled the room once %i!\n", index);
              exit(0);
          }
      }
      else if(strcmp(wall, "no")  == 0){
          printf("Sad... go on\n");
      }
      else {
            index++;
          printf("You can answer only with yes or no!\n");
      }
      wall[0] = '\0';
}
}

我的错误是使用 char wall[2] its small 来表示像 yes 这样的词,我只是把它改成了

char wall[20]

char wall[2]中有多少空间,您认为字符串yes需要多少空间,包括用于标记字符串结尾的零字节?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM