简体   繁体   English

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

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

I have a big Problem.我有一个大问题。 I am a beginner in C, my task is to program a "robot" which is circling my room (theoretically).我是 C 的初学者,我的任务是编写一个在我的房间(理论上)盘旋的“机器人”。 I tried it with this method: index = 0;我用这个方法试过了:index = 0; if you type in "yes" it will add 1 on the index, if you answer with "no" or something else it just print something.如果你输入“是”,它会在索引上加 1,如果你回答“否”或其他东西,它只会打印一些东西。 If index >= 4 it will print something and stop the program.如果 index >= 4 它将打印一些东西并停止程序。

I compiled it on my Raspberry PI 4 with cc and it works totaly fine but if i use Code Blocks (i think it uses GCC) it will set the Integer to 115 (if i type in yes, otherwise it will be 0) and add 1 more to 116, i absolutly dont know why.我用 cc 在我的 Raspberry PI 4 上编译它并且它完全可以正常工作但是如果我使用代码块(我认为它使用 GCC)它会将 Integer 设置为 115(如果我输入是,否则它将为 0)并添加1 到 116,我完全不知道为什么。 But i think its a Problem with the compiler.但我认为这是编译器的问题。 How can solve this?如何解决这个问题?

#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';
}
}

my mistake was to use char wall[2] its to small for words like yes, i just changed it to我的错误是使用 char wall[2] its small 来表示像 yes 这样的词,我只是把它改成了

char wall[20]

How much space is there in char wall[2] , and how much space do you think you need for the string yes , including the zero byte to mark the string's end? char wall[2]中有多少空间,您认为字符串yes需要多少空间,包括用于标记字符串结尾的零字节?

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

相关问题 为什么linux线程函数在windows中有效? - Why does a linux thread function work in windows? 为什么linux编译的程序不适用于Windows - Why does a linux compiled program not work on Windows 为什么此OpenMP代码在Linux上却不能在Windows上运行? - Why does this OpenMP code work on Linux, but not Windows? 用于在文件中打印行数和列数的代码。 为什么它在Windows Mingw gcc环境下却不能在Linux上工作? - Code for printing Number of Lines and Columns in a File. Why does it work on Windows Mingw gcc environment but not on Linux? 为什么代码在Linux上而不在Windows上运行? - Why does the Code run on Linux but not on Windows? 为什么代码在Solaris上有效,但在Linux上无效? - Why does code work on Solaris but not on Linux? 为什么这段代码适用于Linux而不适用于SunOS? - Why does this code work on Linux but not on SunOS? 如何在Windows和Linux下链接到OS C库? - How does linking to OS C libraries under Windows and Linux work? 为什么此代码可与Windows 7上的Visual Studio一起使用而不适用于Linux - Why this code works with visual studio on Windows 7 and doesnt work for linux correctly 为什么我的整数菜单有效但我的字符菜单无效? - Why does my integer menu work but not my character menu?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM