On Thu, 07 Aug 2003 22:05:33 +0900, herrcho wrote:
here is the code
#include <stdio.h>
int main()
{
char *str1="Borland International";
printf("%d\n", strlen(str1));
return 0;
}
i learned when i use strlen i have to include <string.h> but the above
runs o.k without <string.h> i compiled it with gcc -Wall -ansi -pedantic
-o
Could anyone explain about this ?
If you don't #include it, it assumes it's a function that returns int, and
has whatever you gave it as arguments. strlen()'s prototype is:
size_t strlen(const char *)
Since size_t isn't the same as int, this is Undefined Behavior, so
anything can happen. In this case it apparently worked, but you shouldn't
rely on it.
Also, "%d" is incorrect - if you have a C99 compiler, you can use "%zu",
otherwise, cast to the proper type (for "%d", int)
--
Freenet distribution not available
<Shinobi> There are worse things than Perl....ASP comes to mind