There are several things going on here.
As of the 1990 version of C, it was legal to call a function without a visible declaration. Your call strlen(greeting)
would cause the compiler to assume that it's declared as
int strlen(char*);
Since the return type is actually size_t
and not int
, the call has undefined behavior. Since strlen
is a standard library function, the compiler knows how it should be declared and warns you that the implicit warning created by the call doesn't match.
As of the 1999 version of the language, a call to a function with no visible declaration is invalid (the "implicit int
" rule was dropped). The language requires a diagnostic -- if you've told the compiler to conform to C99 or later. Many C compilers are more lax by default, and will let you get away with some invalid constructs.
If you compiled with options to conform to the current standard, you would most likely get a more meaningful diagnostic -- even a fatal error if that's what you want (which is a good idea). If you're using gcc or something reasonable compatible with it, try
gcc -std=c11 -pedantic-errors ...
Of course the best solution is to add the required #include <string.h>
-- and be aware that you can't always rely on your C compiler to tell you everything that's wrong with your code.
Some more problems with your code:
int main()
should be int main(void)
though this is unlikely to be a real problem.
Since strlen
returns a result of type size_t
, use %zu
, not %d
, to print it. (The %zu
format was introduced in C99. In the unlikely event that you're using an implementation that doesn't support it, you can cast the result to a known type.)