The type strlen
returns is size_t
, which is long unsigned int
.
Why compiling the following code does not issue a warning about the signedness of slen
?
char msg[] = "Hello";
int slen= strlen(msg);
(I expected that long unsigned int slen
would have been necessary to avoid such a warning.)
The C and C++ languages let you copy integers between all the different types without a warning (compilers could give you a warning by default, some do with char
, which is a notorious exception).
With gcc
you can use this warning to avoid losing the upper bits (so long long
to int
will give you a warning on 32bit machines).
gcc ... -Wstrict-overflow ...
There is also a flag to prevent automatic conversions:
gcc ... -Wno-int-conversion ...
which is likely to cause a lot of compilation problems, but it can be a good idea to test your code (a lint type of thing).
Looking at the gcc
manual will give you other details about these warnings.
Note that you can also use the -Werror
flag to transform warnings into errors. That way the compilation fails instead of letting you go on with things that you would consider a bug.