calloc is not in stdlib.h. Nothing is in stdlib.h. Nothing is in any .h (or nothing should).
What is in stdlib.h is just (about calloc I mean)
extern void *calloc(size_t nmemb, size_t size);
calloc is in the libc. That is where your program will find it when it will call it.
Strictly speaking, you don't need anything else (I mean, from an execution point of view) to be able to call a function.
All .c codes are compiled to do their task. libc has been compiled long ago. And at linking time (for static libraries, and for dynamic to point to them) and then at run time, all functions from all compiled code are loaded. So your main from your code and calloc will find each other at that time.
The problem is not there.
The problem is that in order to compile your main, the compiler need to know how calloc is supposed to be called.
It needs to know it to check for syntax error. Otherwise you could pass 3 arguments, or only one, to calloc, and the compiler would have no way to know that this is not correct.
You could pass arguments of the wrong type, and likewise.
It needs to know also to know how many bytes it is supposed to push as an argument, and even how it is supposed to pass arguments.
See for example these two codes
one.c
#include <stdio.h>
void printInt(int a, int b){
printf("ints %d %d\n", a, b);
}
void printFloat(float a, float b){
printf("floats %f %f\n", a, b);
}
void printDouble(double a, double b){
printf("doubles %f %f\n", a, b);
}
two.c
#include <stdio.h>
int main(void) {
printInt(1,2);
printInt(1.0, 2.0);
printFloat(1,2);
printFloat(1.0,2.0);
printDouble(1,2);
printDouble(1.0,2.0);
}
Compile them with (depending on your compiler)
gcc -std=gnu99 -o main one.c two.c # There is an implicit -lc here including libc, that contains printf
On my computer it prints
ints 1 2
ints 1034078176 -2098396512
floats 0.000000 0.000000
floats 0.000000 0.000000
doubles 0.000000 0.000000
doubles 1.000000 2.000000
See that it works as intended only for printInt called with ints, and printDouble called with doubles. It fails to cast 1.0 as an int to call printInt, or on the contrary to cast 1 as a double to call printDouble. And For printFloat it fails in all cases, because the compiler assumed wrongly the size of the arguments to be pushed.
But other than that, those 3 functions are called. It is not the the code of the function that is missing. It is the ability of the compiler, when calling them, to call them correctly.
Just add, in two.c the declarations
extern void printInt(int, int);
extern void printFloat(float, float);
extern void printDouble(double, double);
(Or create a one.h containing those, and #include "one.h" in two.c, it leads to the same result)
And now the output is as expected
ints 1 2
ints 1 2
floats 1.000000 2.000000
floats 1.000000 2.000000
doubles 1.000000 2.000000
doubles 1.000000 2.000000
And I haven't even started with types declarations.
#include are not meant to provide libraries, and the functions that are in them. That you do while linking, adding .o and -lsomelib to the liking command line (or using other way, depending on your compiler).
#include are there to provide codeless declarations that the compiler needs to know how to call those functions.
calloc?”: A warning that there is an implicit declaration of a function does not mean the compiler knew about your function previously. If you writey = foo(x);, then, when the compiler seesfoo(, it can see you are usingfooas a function and that you have not declared it, so it can warn you aboutfoofrom that, not from prior knowledge offoo. In the case ofcalloc, the compiler may have prior knowledge ofcalloc. This is because the names of standard library routines are reserved by the C standard for use with file scope.-Wall -Wextra -pedanticto yourgcc/clangcompile string (also consider adding-Wshadowto warn on shadowed variables and-Werrorto treat warnings as errors). For VS (cl.exeon windows), use/W3. All other compilers will have similar options. Read and understand each warning -- then go fix it. The warnings will identify any problems, and the exact line on which they occur. You can learn a lot by listening to what your compiler is telling you.