Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of undeclared identifier 'true'

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);
    }
}
like image 384
gadgetmo Avatar asked Nov 10 '12 14:11

gadgetmo


People also ask

What is use of undeclared identifier?

The identifier is undeclaredIf the identifier is a variable or a function name, you must declare it before it can be used. A function declaration must also include the types of its parameters before the function can be used.

What does undeclared identifier mean in Delphi?

"Undeclared identifier" means that Delphi cannot find the declaration that tells it what showmassage is, so it highlights it as an item that hasn't been declared.


3 Answers

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.

like image 198
md5 Avatar answered Oct 07 '22 17:10

md5


C has no built-in boolean types. So it doesn't know what true is. You have to declare it on your own in this way:

#define TRUE 1
#define FALSE 0

[...]
while (TRUE) {
     [...]
}
like image 23
Adam Sznajder Avatar answered Oct 07 '22 15:10

Adam Sznajder


Include stdbool.h to use C99 booleans.
If you want to stick with C89 define it yourself:

typedef enum
{
    true=1, false=0
}bool;
like image 4
Ramy Al Zuhouri Avatar answered Oct 07 '22 17:10

Ramy Al Zuhouri