Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

270
Views
What is the proper equivalent of "while(true)" in plain C?

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" ?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Usually nowadays I see

while(1) {
    ...
}

It used to be common to see

for(;;) {
    ...
}

It all gets the point across.

over 4 years ago · Santiago Trujillo Report

0

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.

over 4 years ago · Santiago Trujillo Report

0

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

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!