Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

276
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda