3

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?

DumpDaCode
  • 53
  • 7
  • 3
    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. – kaylum Jun 09 '20 at 05:29
  • 1
    Relevant info: [What is the difference between a definition and a declaration?](https://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration/1410632#1410632) – kaylum Jun 09 '20 at 05: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) – DumpDaCode Jun 09 '20 at 05:56
  • 1
    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) – kaylum Jun 09 '20 at 06: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? – DumpDaCode Jun 09 '20 at 06:11

3 Answers3

3

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

Jeroen3
  • 919
  • 5
  • 20
  • A curious question I have now. Based on which header file included (standard ones) does linker links all the .a or .so file or it knows which one to link with. – DumpDaCode Jun 09 '20 at 07:55
  • 3
    @rajiv None. The linker is fed with objects (.a and .o) which it links together to your executable. Preprocessor (.h) -> Compiler (.c) -> Linker (.o) -> application – Jeroen3 Jun 09 '20 at 08:11
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).

2

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

Shujaul Hind
  • 105
  • 1
  • 9