DEV Community

Discussion on: The different files of C

konstantinklima profile image
Konstantin Klima

Well, there are two terms here which are distinct - declaration, as Nested said, is giving a prototype of a function or a name of a variable.

void f()

Defining is giving the function it's body (the code of the function).

void f() { return; }

The declaration tells the compiler what to expect and a promise that we will give him that exact thing at some point later.

We can use this to our advatange - for instance by declaring all the functions at the top of the file and then defining them bellow, which helps us better organize the file for human reading.

Similarilay, you put the declarations in the header, and the definitions in the .c file.

You can also both declare and define the functions in the header file - these are called header only libraries (many cool ones available online). The problem with this is that if you have A.c, B.c, C.c, all of them will copy-paste all the functions from the h file into the end result, which can cause slower compilation and potential conflicts. This can by solved by using #ifndef (guards) or the #pragma once preprocessior directive.