DumpDaCode
DumpDaCode

Reputation: 63

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

I'm new to C and I came across this statement:

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

My question is then, where are standard functions defined?

Upvotes: 4

Views: 3960

Answers (3)

Jeroen3
Jeroen3

Reputation: 935

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

You declare the existence of a function in the header. For all dependents to see.
You implement (define) the function in the source file. Dependents cannot see this.
The header file is the API or your C file for use elsewhere in your application, hiding the implementation.
The exception for inline functions has to do with the fact that when compiling the dependent, the compiler must know what to put instead of the function call.

Functions for (private) use only inside your source file do not need to be declared in the header file. Why tell other code about it?

My question is then where are standard functions defined?

In the compiled library. You only know that they exist, not where they exist. The implementation is hidden from you. The linker connects the dots.

Upvotes: 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.

  1. 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)
    {
       foo();
    }
    

    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.
    }
    

    foo.h:

    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).

Upvotes: 3

Shujaul Hind
Shujaul Hind

Reputation: 105

Yes functions are declared in .h file and they are defined in .c file

Upvotes: 2

Related Questions