Should I define functions in .h file or just declare them?

You are misunderstanding that statement. It is saying that header files should only in general contain function declarations and not function definitions. Function definitions are in the .c files. That would be the same for standard functions as well as your own functions.

Commented Jun 9, 2020 at 5:29 Relevant info: What is the difference between a definition and a declaration? Commented Jun 9, 2020 at 5:30

@kaylum To be more elaborative when we include stdio.h then only the declarations of functions and global variables are included from the header file, right? This is where my confusion begins. From where does, linker comes to know about their declarations. (the whole argument started with standard headers)

Commented Jun 9, 2020 at 5:56

The linker looks for libraries in standard locations as well as any locations you tell it to on the command line (e.g. -L option for gcc ). The libraries have binary objects which contain the compiled function definition code. (roughly speaking)

Commented Jun 9, 2020 at 6:03

@kaylum ok. So, when i would make my own header file then i need to only declare the functions in that file and make a single file that contains all the declarations and compile it. Then tell the compiler from where it needs to link the object code for it. Right?

Commented Jun 9, 2020 at 6:11

3 Answers 3

"Functions need to be declared in .h files and not defined with exception of inline functions"

Unknown Source

Unfortunately, this quote is ambiguous and not entirely correct as well.

    Functions do not necessary need to be declared in .h files. You can declare a function in .c files as well, or even omit the declaration entirely (dependent upon your coding style and the place of the function definition). Note for the latter, that the compiler reads a file from top to bottom. For example, This is the content of the source file foo.c :

void foo (void) < // absolute foo / translated: It really does not make anything useful. // this function is only to illustrate the omission of the prototype. >int main (void)

To call foo() we need no declaration of foo() at all because the definition is before its use in the source code (compiler reads from top to bottom). Would it be behind, we would need a declaration to tell the compiler what foo() is, f.e.:

void foo (void); // foo() needs to be declared. Else the compiler does not know // what foo is when foo is called in main. int main (void) < foo(); >void foo (void) < // absolute foo. >

It is just a common use to place the declaration/ prototype of a function in a separate header, but you actually do not need to do so. This is the header version: foo.c:

#include "foo.h" // We need to include the header to gain the prototype. // Note that `foo.h` needs to be in the same directory // as `foo.c` in this case. Else you need to specify the // full path to foo.h . int main (void) < foo(); >void foo (void) < // absolute foo. >
void foo (void); 

What the quote really means is:

  1. A function definitely should not be defined in an .h file (as these are headers, not source files), with the only exception of inline functions, which indeed can be defined in .h files.

"My question is then, where are standard functions defined?"

That is a completely different question and implementation-specific. The functions are stored in an either fully or semi-compiled form. I can not answer it here without the focus to any specific implementation (OS, compiler).