Alex Rivera | Logout

Use of undeclared identifier 'true'

Asked 2012-11-10T14:04:23.113
45

Why am I getting this error:

infinite.c:5:12: error: use of undeclared identifier 'true'
    while (true) {

1 error generated.
make: *** [infinite] Error 1

... when I try to compile this simple code for an infinite loop?

#include <stdio.h>

int main(void) {
    int x = 0;
    while (true) {
        printf("%i\n", x);
    }
}
Edit
Report

1 Answer

65

The identifier true is not declared by default. To use it, two solutions :

  1. Compile in C99 and include <stdbool.h>.
  2. Define this identifier by yourself.

However, the infinite loop for (;;) is often considered as better style.

answered 2012-11-10T14:39:22.267

Your Answer