Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C语言中突破while循环_C_Loops_While Loop - Fatal编程技术网

如何在C语言中突破while循环

如何在C语言中突破while循环,c,loops,while-loop,C,Loops,While Loop,为什么我不能在C中打破这个循环 char command[1]; scanf("%s", command); while(1){ scanf("%s", command); if(command == "q") break; } 根据您尝试执行的操作,您可能需要扫描格式的%c而不是%s。这将得到一个字符。然后您可以像这样

为什么我不能在C中打破这个循环

        char command[1];
        scanf("%s", command);
        while(1){
                scanf("%s", command);
                if(command == "q")
                        break;
        }

根据您尝试执行的操作,您可能需要扫描格式的%c而不是%s。这将得到一个字符。然后您可以像这样与q进行比较:命令[0]='q'


但您可能需要一个多字符的命令,在这种情况下,strcmp和更长的缓冲区就是您想要的。

'command'是一个变量,它包含堆段中某个地方的内存块地址,而q由编译器替换为文本段中的内存块地址,所有常量字符串都保存在文本段中。这意味着“==”运算符比较进程内存不同段中的两个地址。因此,这永远不会是真的。
您必须使用strcmp或strncmp函数来比较字符串,而不是这些字符串的地址

因为它充满了未定义的行为。字符串中没有提供足够的空间;你不能用strcmp比较那样的字符串。您需要检查scanf的结果;不清楚连续两次给scanf打电话是否是个好主意。