2

Implicit conversion loses integer precision:
'unsigned long' to 'int'

I want to get the length of words[] but it shows me this. How can I fix it?

int main(int argc, const char * argv[]) {
    
    char test[] = "ls test";
    f_sparce_arg(test);
    return 0;
}
char * f_sparce_arg(char words[]){
    int words_length;
    words_length = strlen(words);   /* warning: Implicit conversion 
                                       integer precision: 'unsigned 
                                       long' to 'int' */
    
    printf("%d", words_length);
    return "";
}
// warning: Implicit conversion integer precision: 'unsigned long' to 'int'
Community
  • 1
  • 1
Chener Zhang
  • 129
  • 9
  • This isn't an error and you can do fine with it. The compiler is just alerting you that the value returned by strlen() whose type is unsigned long is implicitly being converted to type int which is the type of the variable you are using to store that value. – machine_1 Oct 05 '19 at 16:34

3 Answers3

4

What strlen function returns is size_t, so you should use it to store its return value.

char * f_sparce_arg(char words[]){
    size_t words_length; /* use proper type */
    words_length = strlen(words);

    printf("%zu",words_length); /* the format to print size_t value is %zu */
    return "";
}

Unfortunately, %zu format is not supported in some compilers or libraries. Another way is, if the string won't be so long, to cast the return value to int.

char * f_sparce_arg(char words[]){
    int words_length;
    words_length = (int)strlen(words); /* explicitly cast the result to avoid warnings */

    printf("%d",words_length);
    return "";
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • If it is not supported, you can use `%lu`, but it is still not guaranteed to work. – Arusekk Oct 05 '19 at 17:24
  • @Arusekk Another way is using `size_t` and casting: `printf("%lu", (unsigned long)words_length);`. This way have some risk of causing overflow, but it won't invoke undefined behavior. – MikeCAT Oct 06 '19 at 12:53
2

This is not an error and the code will work absolutely fine given you have added a function declaration and the preprocessor directives. it is a warning that your complier gives you that there might be some data altering in conversion as the conversion of unsigned to signed is specific to compiler and not language.

C have a data type size_t which is capable of storing the largest possible data size in C and is defined in several preprocessor like <stddef.h>, <stdio.h>, <stdlib.h>, <string.h>, <time.h>, <wchar.h> and is an alias for unsigned int in 32 bit compiler and is an alias for unsigned long long in a 64 bit compiler. It is a good practice to add additional information like unsigned and size_t for cross platform performance which is require while building a project.

Aditya Gaddhyan
  • 354
  • 1
  • 14
1

I think you could just fix it by swap int into unsigned int. (Not perfect solution) An int variable may be a negative value, so it's not safe to assign any value to this variable. If you assign unsigned value to int, it may cause overflow, which is very dangerous.

FL1NT
  • 101
  • 1
  • 2