Why does strlen function work without #include <string.h>?

Quick question:

strlen[char*] works great regardless of whether I am #include <string.h> or not

All I get from the compiler is a warning about an implicit declaration, but functionally it works as intended.

Why is this?

+4
source share
7 answers

Function prototypes in C are optional . They are useful for the compiler, so that it can check the type for types that are passed to them. When you do not include string.h , the function uses the default premise, so you get a warning.

+3
source

When calling undefined behavior, you can assume that the program behaves as you expected, with your system and with the current version of the libraries and system software that you installed. This does not mean that everything is in order. Actually, the correct C99 compiler should not allow declarations of implicit functions; he should give you an error message.

+6
source

If a function is called without a prototype in scope, the compiler will generate code to call a function that accepts any type of parameter and accepts an integer result. If the parameters correspond to those that the function expects, and if the function returns its result in a way that the call code (*) can process, then everything will be fine. The purpose of prototyping is to ensure that arguments are converted to expected types, if possible, and that compilation will fail if they cannot be converted. For example, if a non-prototyped function expects an argument of type "long" and one tries to pass "int", any of the following can happen:

  • The program may crash right
  • Things may work as expected
  • The function can be executed as if some arbitrary parameter value was passed to it
  • A program can continue to work, but with arbitrary values ​​that distort any or all of the program variables.
  • A computer can make demons fly out of the nose of a programmer

In contrast, if the function was prototyped, then the compiler will be guaranteed to do everything necessary to convert "int" to "long" before calling the function.

When C was originally conceived, no prototypes existed, and the programmer was responsible for ensuring that all arguments were passed with the exact expected types. In practice, this is a real pain to ensure that all arguments to the function are always the exact correct types (for example, when passing the value of five functions that expect a long one, you need to write it as "5L" or "(long) 5"). In fact, there is never (**) reason to rely on implicit argument types, with the exception of variational functions.

(*) Any of the things that can happen with the wrong types of parameters can happen with the wrong types of return data, unless it is expected that the function will return 'int' and the actual return value of the function will be placed in 'int', the results are likely to be correct than using the wrong parameter types.

(**) The only exceptions I can think of would be if someone programmed some really old equipment for which there was no compiler compatible with the prototype, or if he programmed for code golf or similar competition. In the latter case, I am considering solving puzzles, not programming, and I do not know what hardware people will be interested in using for which the former condition is applicable.

+3
source

Because it is an announcement, i.e. equal to the so-called default ad. The compiler expects that any unknown function will return the int and expect parameters that are passed when the function is first used in the code.

+1
source

This is usually due to the fact that string.h is included in another header file that you included ALSO. Obviously, bad practice assumes that you do not need to include something just because you are doing something else, but most likely it is responsible for this effect.

+1
source

I guess this is because in C int is the default data type returned by the function. Can you give a more complete code example.

0
source

The function protocol is present in include files. Therefore, even if you do not include these files, a fixed prototype is written int function_name(); . Now the strlen() code is present in the library files that are linked at runtime, so the function gives the correct output (only if this is the only function with a fixed prototype int function_name(); ).

0
source

All Articles