C语言 如何终止 Turbo C 中的无限循环?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6025726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I terminate an infinite loop in Turbo C?
提问by aer
I get stuck in an infinite loop. How can I terminate this loop? I tried to use/press Cntrlcbut nothing happens. I don't know how to stop it.
我陷入了无限循环。我怎样才能终止这个循环?我尝试使用/按下Cntrlc但没有任何反应。我不知道如何阻止它。
main()
{
while (1)
{
char ch;
printf("Enter a character: \n");
ch = getche();
printf("\nThe code for %c is %d.\n", ch, ch);
}
}
回答by paxdiablo
CTRLBREAKwill probably work for this. I have a vague recollection that CTRLCdid not always work with the Borland products.
CTRLBREAK可能会为此工作。我有一个模糊的回忆,它CTRLC并不总是适用于 Borland 产品。
Though, that wasa long time ago so I had to retrieve that from very deep memory, which may have faded somewhat :-)
不过,那是很久以前的事了,所以我不得不从很深的记忆中检索它,这可能已经有些褪色了:-)
My question for you is: Why is anyone still using Turbo C when much better and equally cheap solutions are available? Like gcc (such as in Code::Blocks) or even Microsoft Visual C Express.
我要问你的问题是:当有更好且同样便宜的解决方案时,为什么还有人使用 Turbo C?像 gcc(例如在 Code::Blocks 中)甚至 Microsoft Visual C Express。
回答by cidermonkey
you need a condition to break out of your while loop.
你需要一个条件来跳出你的 while 循环。
so like,
所以喜欢,
main()
{
char ch = ' ';
while (ch != 'q')
{
printf("Enter a character: \n");
ch = getche();
printf("\nThe code for %c is %d.\n", ch, ch);
}
}
would break out if the entered char was 'q', or if you insist on while(1), you could use the "break" keyword:
如果输入的字符是'q',或者如果你坚持while(1),你可以使用“break”关键字:
main()
{
while (1)
{
char ch;
printf("Enter a character: \n");
ch = getche();
printf("\nThe code for %c is %d.\n", ch, ch);
if (ch == 'q')
break;
}
}
回答by Ninad Ghodinde
If you want to just pause your infinite loop in Turbo C then press the BREAK. If you want to get back to the editor of your program in Turbo C then press CTRL+BREAK. It will return back to editing your program.
如果您只想在 Turbo C 中暂停无限循环,请按BREAK。如果您想返回 Turbo C 中的程序编辑器,请按CTRL+ BREAK。它将返回编辑您的程序。
Yes, I tried this and it works!
是的,我试过这个,它有效!
回答by Dave
CTRL-Break, Break and CTRL-C didn't work for me, but CTRL-ESC-ESCdid! (This was tested with almost identical code in Borland C++ 3.1).
CTRL-Break、Break 和 CTRL-C 对我不起作用,但CTRL-ESC-ESC起作用了!(这是在 Borland C++ 3.1 中用几乎相同的代码测试的)。
回答by Yash Rajpurohit
There is no way to stop an infinite loop. However, you can add a condition inside of the loop that causes it to break, or you can call the exit() function inside of the loop, which will terminate your program.
没有办法停止无限循环。但是,您可以在循环内添加一个条件以使其中断,或者您可以在循环内调用 exit() 函数,这将终止您的程序。

