我正在做一个命令解释器,其中一个命令必须有从程序退出的结果。当用户插入“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/