Do all C functions need to be declared in a header file

Do I need to declare all functions I use in a .c file in a header file, or can I just declare and define right there in the .c file? If so, does a definition in the .c file in this case count as the declaration also?

309k 27 27 gold badges 197 197 silver badges 350 350 bronze badges asked Feb 12, 2016 at 5:53 UnworthyToast UnworthyToast 825 5 5 gold badges 11 11 silver badges 22 22 bronze badges

The compiler doesn't care where the declarations are. Header files are simply merged into the .c file, and then it processes them the same way. Header files exist for convenience, so you don't have to repeat the same thing in multiple files.

Commented Feb 12, 2016 at 5:56

6 Answers 6

Do I need to declare all functions I use in a .c file in a header file, or can I just declare and define right there in the .c file?

You used "use" in the first question and "define" in the next question. There is a difference.

void foo()

Here, foo is defined and bar is used. You should declare bar . If you don't declare bar , the compiler makes assumptions about its return type.

You can declare bar in the .c file or add the declaration in a .h file and #include the .h file in the .c file. Whether you use the first method or the second method is up to you. If you use the declaration in more than one .c file, it is better to put that in a .h file.

You can define foo without a declaration.

If so, does a definition in the .c file in this case count as the declaration also?

Every function definition counts as a declaration too.

answered Feb 12, 2016 at 6:05 206k 14 14 gold badges 162 162 silver badges 283 283 bronze badges

For the compiler, it does not matter if a declaration occurs in a .h or a .c file, because the compiler sees the preprocessed form.

For the human developer reading and contributing to your code, it is much better (to avoid copy&pasting the same declaration twice) to put the declaration of any function used in more than one translation unit (i.e. .c file) in some #include -d header.

And you can define a function before using it.

BTW, you might even avoid declaring a function that you are calling (it defaults to returning int for legacy purposes), but this is poor taste and obsolete way of coding (and most compilers can emit a warning in that case).

answered Feb 12, 2016 at 5:55 Basile Starynkevitch Basile Starynkevitch

No, it is not necessary.

The reason of the header files is to separate the interface from the implementation. The header declares "what" a class (or whatever is being implemented) will do, while the .c file defines "how" it will perform those features.

This reduces dependencies so that code that uses the header doesn't necessarily need to know all the details of the implementation and any other classes/headers needed only for that. This will reduce compilation times and also the amount of recompilation needed when something in the implementation changes.

answered Feb 12, 2016 at 6:01 Hamza Anis Hamza Anis 2,565 1 1 gold badge 27 27 silver badges 38 38 bronze badges

The answer to both questions is yes. You can declare c-functions in both header and .c file. Same with definition. However, if you are defining it in header file, you may have slight problems during compilation.

answered Feb 12, 2016 at 5:55 user763410 user763410 562 6 6 silver badges 23 23 bronze badges

By default functions have external linkage. It means that it is supposed that functions potentially will be used in several compilation units.

However sometimes some auxiliary functions that form implementations of other functions are not designed to be used in numerous compilation units. Such functions declared with keyword static have internal linkage.

Usually they are declared and defined inside some .c module and are not visible in other compilation units.

answered Feb 12, 2016 at 6:29 Vlad from Moscow Vlad from Moscow 309k 27 27 gold badges 197 197 silver badges 350 350 bronze badges

One occasion that requires functions to be declared in a separate header is when one is creating a library for other developers to use. Some libraries are distributed as closed source and they are provided to you as a library file (*.dll / *.so . ) and a header.

The header file would contain declarations of all publicly accessible functions and definitions of all publicly required structures, enums and datatypes etc.

Without this header file the 3rd party library user would not know how to interface with the library file and thus would not be able to link against it.

But for small, trivial C programs that are not intended for use by other people, no you can just dump everything into a C file and build it. Although you might curse yourself years later when you need to maintain that code :)