c - C 中使用 char 退出

标签 c char exit

我正在做一个命令解释器,其中一个命令必须有从程序退出的结果。当用户插入“q”时,程序应该停止执行。

我写了这个,但看起来没什么用。

提前致谢。

#include <stdio.h>
#include <stdlib.h>

void exiting (char x){
if (x=='q') exit(0);

int main(){
char x;
scanf("%c", x);
exiting(x);
return 0;
}

最佳答案

#include <stdio.h>
#include <stdlib.h>

void exiting (char x){
    if (x=='q') exit(0);
}

int main(){
    char x;
    scanf("%c", &x); //scanf requires a pointer since it's the only C way to change a value of a variable within a function
    exiting(x);
    return 0;
}

或者,如果您希望程序一直运行直到您按“q”,则代码如下:

#include <stdio.h>
int main(int argc, char* argv[])
{
    char x;
    do
    {
    scanf("%c", &x);
    }while(x != 'q');
}

关于c - C 中使用 char 退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29214100/

相关文章:

子进程无法正常工作

无法在 cp 程序中打开文件

c - 重新分配原始数组后,数组元素会发生什么?

c - 在 C 中,两个相同的链并不将自己标识为相等

c++ - 如何以编程方式缓慢移动窗口,就好像用户正在做一样?

c++ - char 数组末尾的奇怪字符

c - 带 EOF 的 While 循环始终提示输入

jquery - .getJSON请求永不退出

c++ - 从子进程调用 _exit(errno) 时出现错误状态

shell - 返回 shell 脚本中的最后一个非零返回码