0

In my code, I call functions from the string.h library (specifically, strcmp()), but I forgot to add the string library. Even without including the string library, my code compiles and runs properly as if I had included the string library.

What is happening?

5
  • 3
    Enable all warnings for your compiler. It will tell you it assumes default int arguments and return types. Commented May 1, 2016 at 1:35
  • 1
    It's possible that it gets #included indirectly. Commented May 1, 2016 at 1:36
  • What do you mean when you say you forgot to add the string library? Commented May 1, 2016 at 1:45
  • When I called the string.h functions, I didn't initially plan to use them so I didn't add the library beforehand. After I finished writing the code, I compiled it. The code compiled without error and behaved as I wanted it to even though the string.h library wasn't included. Commented May 1, 2016 at 1:46
  • Perhaps you #include another file that happens to #include <string.h> Commented May 1, 2016 at 1:53

4 Answers 4

4

A header file (e.g., string.h) only includes function declarations: that is, the return type and types and number of parameters.

The functions are defined in the C library (which is linked against your code by default), so they are are available to you whether or not you #include <string.h>. However, if you fail to #include the header file, the compiler might warn you about missing function declarations, and it can also cause problems if the return type of a function is other than int (the default for functions not otherwised declared in C).

Sign up to request clarification or add additional context in comments.

2 Comments

There can be problems with arguments, too -- the conversions performed in the absence of a prototype may be different than the ones performed when a prototype is in scope.
Thanks, that too. I knew I was forgetting something!
3

Starting with the 1999 ISO C standard, calling a function with no visible declaration is a constraint violation, requiring a diagnostic. The diagnostic may be a non-fatal warning.

Some C compilers do not enforce the newer C99 rules by default. For example, until recently gcc's default behavior was -std=gnu89, which supports the 1989/1990 C standard with GNU-specific extensions.

Under the older rules, if you call a function with no visible declaration, an implicit declaration is created assuming that the function returns int and expects the number and types of arguments you've passed it. If that assumption is incorrect, the call's behavior is undefined. It happens that strcmp() returns an int, so if the compiler accepts the call you'll probably get away with it.

You should find out how to get your compiler to at least warn you about calls to undeclared functions. Once you've confirmed that it will do so, you should add the requires #include <string.h> to your code.

Note that the #include directive only includes the header file that declares strcmp and other standard functions, not the library. The definitions of those functions (the code that implements them) is commonly included as part of the C standard library. Linking your program to the standard library is handled by the linker, not by the compiler, and it's usually done implicitly (because you asked to compile and link a C program). (The math library, depending on the implementation, might not be linked automatically, but the implementation of the string functions almost always is.)

Comments

0

The string library is in fact included if you include the stdio library. But the stdio does not include the string library(not directly)

By default compiler include all necessary header file and program will successfully run.

Comments

0

Which Compiler you are using ? you should use C99 compiler with -strict flags and -error flags then compiler will give you error if you call a function without including header file..

Error will look like this implicit declaration of strcmp() found

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.