22

Since C doesn't have bools, what is the proper variable to put in place of true in an algorithm that uses

do
{
   // ... 
} while(true);

???

Should a proper C programmer do

do
{
   // ... 
} while(1);

or is there a specific variable reserved to mean "something that is not zero/NULL" ?

  • possible duplicate of [Using boolean values in C](http://stackoverflow.com/questions/1921539/using-boolean-values-in-c) – BJ Myers Jun 06 '15 at 04:30
  • 2
    Modern C does have bools (http://stackoverflow.com/questions/1608318/is-bool-a-native-c-type/1608350#1608350). – Paul Hankin Jun 06 '15 at 04:30
  • 2
    `#include ` -- Congratulations, you now have `bool`, `true` and `false` in 2015 (and 1999, for that matter)! – autistic Jun 06 '15 at 06:02

4 Answers4

35

Usually nowadays I see

while(1) {
    ...
}

It used to be common to see

for(;;) {
    ...
}

It all gets the point across.

U2EF1
  • 12,907
  • 3
  • 35
  • 37
  • Some compilers issue a warning for loop condition expressions that are compile-time constant true; so for that reason prefer `for(;;)` in both C and C++. – Clifford Jun 06 '15 at 06:30
  • `for(;;)` is more language independent (e.g. also works in Java) and is more intuitive in my opinion, I call it the `forever`. – maraca Jun 06 '15 at 09:25
  • 2
    `#define EVER ;;`, `for(EVER) {}` – Corsaka Oct 11 '21 at 16:51
15

Your question isn't really about bool (which modern C does have if you #include <stdbool.h>), it's about the best way to write an infinite loop.

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 for loop header can be omitted; if the second expression, which controls when the loop continues to execute, is omitted, it defaults to 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.

I personally wouldn't use a do/while loop, because the condition is at the bottom.

There are trickier ways to write an infinite loop (while (1 + 1 == 2) et al, but none of them are really worth the effort.

Either while (1) or for (;;) will be perfectly clear to anyone with a good understanding of C.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
8

C has stbool.h header file to use bool variables.

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 C99 has support for bool variables but C89/90 does not have.

If you are using C89/90 then you can use typedef or constants as mentioned in one of the answers here or you can use enums as well like this

   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.

aakansha
  • 695
  • 5
  • 18
7

If you're using 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.

Or (as mentioned elsewhere here) if using c99:

#include <stdbool.h>

My experience of universities lately, is they require you to use c89.

http://en.wikipedia.org/wiki/C_data_types#stdbool.h