C语言 普通 C 中“while(true)”的正确等价物是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30678905/
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
What is the proper equivalent of "while(true)" in plain C?
提问by Microsoft Orange Badge
Since C doesn't have bools, what is the proper variable to put in place of truein an algorithm that uses
由于 C 没有bools,在使用strue的算法中放置什么合适的变量
do
{
// ...
} while(true);
???
???
Should a proper C programmer do
一个合适的 C 程序员应该这样做吗
do
{
// ...
} while(1);
or is there a specific variable reserved to mean "something that is not zero/NULL" ?
或者是否保留了一个特定的变量来表示“不为零/NULL 的东西”?
回答by U2EF1
Usually nowadays I see
现在通常我看到
while(1) {
...
}
It used to be common to see
以前经常看到
for(;;) {
...
}
It all gets the point across.
这一切都说明了这一点。
回答by Keith Thompson
Your question isn't really about bool(which modern C doeshave if you #include <stdbool.h>), it's about the best way to write an infinite loop.
你的问题是不是真正的bool(其中现代C也有,如果你#include <stdbool.h>),它是关于写一个无限循环的最佳方式。
Common idioms are:
常见的成语有:
while (1) {
/* ... */
}
and
和
for (;;) {
/* ... */
}
The latter looks a little obscure, but it's well-defined. Any of the three expressions in a forloop header can be omitted; if the second expression, which controls when the loop continues to execute, is omitted, it defaults to true.
后者看起来有点晦涩,但定义明确。for循环头中的三个表达式中的任何一个都可以省略;如果省略控制循环何时继续执行的第二个表达式,则默认为 true。
while (1)is probably the most straightforward method -- but some some compilers might warn about a condition that's always true. for (;;)likely avoids that, since there is no (explicit) expression to warn about.
while (1)可能是最直接的方法——但一些编译器可能会警告总是为真的条件。for (;;)可能会避免这种情况,因为没有(显式)表达式要警告。
There are trickier ways to write an infinite loop (while (1 + 1 == 2)et al, but none of them are really worth the effort.
有一些更棘手的方法来编写无限循环(while (1 + 1 == 2)等,但它们都不是真正值得付出努力的。
回答by Yvette
If you're using c89:
如果您使用的是 c89:
Create a boolean definition:
创建一个布尔定义:
typedef int bool;
#define true 1
#define false 0
Or constants:
或常量:
/* Boolean constants. */
#define TRUE 1
#define FALSE 0
This gives the int a meaning for you.
这使 int 对您有意义。
Or (as mentioned elsewhere here) if using c99:
或者(如此处其他地方所述)如果使用 c99:
#include <stdbool.h>
My experience of universities lately, is they require you to use c89.
我最近在大学的经验,是他们要求你使用c89。
回答by aakansha
C has stbool.hheader file to use boolvariables.
C 有stbool.h头文件来使用bool变量。
So this works
所以这有效
#include <stdio.h>
#include<stdbool.h>
int main(void) {
int i=1;
while(true)
{
if(i==3){
break;
}
printf("%d\n",i);
i++;
}
return 0;
}
Output
输出
1
2
note:Modern C99has support for boolvariables but C89/90does not have.
注意:现代C99支持bool变量,但C89/90不支持。
If you are using C89/90 then you can use typedefor constantsas mentioned in one of the answers here or you can use enumsas well like this
如果您使用的是 C89/90,那么您可以使用typedef或constants此处的答案之一中提到的,或者您也可以enums像这样使用
typedef enum {false, true } bool;
bool b=true;
int i=1;
while(b)
{
if(i==3){
break;
}
printf("%d\n",i);
i++;
}
output
输出
1
2
You can check this bool in C
Hope it helps you.
希望对你有帮助。

