Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

273
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda