I'm new to C programming and I have questions about the header files inclusion part.
(1) I know that by convention, a header file shouldn't include function definitions. But in this simple code:
#include<stdio.h>
int main(){
printf("123");
}
printf function is declared in stdio.h, however I didn't manually define the printf function. Why does the code compile successfully? Shouldn't it throw a compilation error something like function printf is not defined?
(2) Suppose that I have a foo.h header file, which declares function foo
int foo(int num);
I have a main.c file below in the same directory as foo.h:
#include <stdio.h>
#include "foo.h"
int main(){
printf("%d",foo(123));
}
My question is where should I define function foo? Is there any other place besides inside main.c?
If not, what's the point of using header files? Because in my mind, it's like: every time we want to implement a function declared in a header file, we have to define it in the .c file.
Why not define (and thus declared) function foo in main.c, directly?