Home > Blockchain >  Questions about C strlen function
Questions about C strlen function

Time:11-08

I tried to compare with strlen(string) with -1 but different methods gave different results:

char string[] = {"1234"};
int len = strlen(string);
int bool;
bool = -1 < strlen(string);
printf("%d",bool); //bool=0
bool = -1 < len;
printf("%d",bool); //bool=1

Assigning values to len and then comparing them gives the correct result, but I don't understand why directly comparing with strlen doesn't work.

CodePudding user response:

The function strlen has the unsigned return type size_t

size_t strlen(const char *s);

Usually the type size_t is an alias for the type unsigned long.

In any case the rank of the unsigned type size_t is not less than the rank of the signed type int. So due to the usual arithmetic conversion an operand of the signed type int is converted to the type size_t and if the operand of the type int has a negative value than due to propagating the sign bit it becomes a big unsigned value.

So in this expression

-1 < strlen(string)

of the type size_t (the common type of the expression) the left operand after conversion to the type size_t becomes greater than the right operand.

Consider the following demonstration program.

#include <stdio.h>

int main( void )
{
    printf( "-1 = %d\n", -1 );
    printf( "( size_t )-1 = %zu\n", ( size_t )-1 );
    return 0;
}

Its output might look like

-1 = -1
( size_t )-1 = 4294967295

CodePudding user response:

Sign problem I guess, comparing to strlen must cast -1 to unsigned, so all bits set to 1, so whatever value is compared with, expression evaluates to 0.

While comparing with a typed variable, problem can't occur since compiler doesn't have to guess which operand has to be casted to other type.

What happens if you compile with -Wall -Wextra -Werror? Error because of comparaison between invalid types?

  • Related