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

166
Visualizações
What will be the value of strlen(str)- 1 in a 'for' loop condition when str is empty?

I am analyzing a scenario:

char str[] = ""; // Understand

If I understand strlen(str), it comes out to be 0. This is OK.

printf(" %d,  %ul,  %u, %d,  %ul, %u", 
    strlen(str),
    strlen(str),
    strlen(str),
    strlen(str) - 1,
    strlen(str) - 1,
    strlen(str) - 1);

Output is:

0, 0l, 0, -1, 4294967295l, 4294967295

I understand these as well.

for (int i = 0; i < strlen(str) - 1; i++)
{
}

Here I don't understand what the value of strlen(str) - 1 would be in the for loop condition.

strlen(str) - 1 is giving the value 4294967295 in the for loop. Why is that? Why not -1?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

strlen returns size_t which is an unsigned integer. So strlen(str)-1 would produce SIZE_MAX (the maximum value size_t can hold) if strlen(str) is 0.

You should be using %zu to print size_t values.

over 4 years ago · Santiago Trujillo Relatório

0

This statement

printf(" %d,  %ul,  %u, %d,  %ul, %u", strlen(str),strlen(str),strlen(str),strlen(str)-1,strlen(str)-1,strlen(str)-1);

shows that when strlen( str ) - 1 is outputed like unsigned integer for example using format specifier %ul its value is 4294967295l

In the condition of the loop

for (int i=0;i<strlen(str)-1;i++)

the compiler has to determine the common type of the left and right operands that to determine the type of the result of the condition

i<strlen(str)-1

The right operand strlen(str)-1 has type size_t (the return type of function strlen is size_t). It is unsigned integer type usually that corresponds to unsigned long. It can not have negative values. Any value that stored in an object of this type is interpretated as a non-negative value and as the output shows the value of strlen(str)-1 is equal to 4294967295l. (The actual value you could get if you used type specifier %zu because it may not be excluded that size_t can correspond even to unsigned long long)

The right operand has type int. Its rank is at least not greater than the rank of size_type. So the both operands are converted to type size_t and have non-negative values.

This procedure of determining of the common type is called the usual arithmetic conversions. It is obvious that 4294967295l is greater than 0. Thus the loop will iterate 4294967295l times if it does not have a break statement.

You could get the expected result if you rewrite the condition in the loop the following way

for ( int i = 0; i < ( int )strlen( str ) - 1; i++ )
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