The convention in C/C++ when implementing a module/library is to divide it into headers (.h) and source files (.c, .cc, .cpp). You typically include only the bare minimum interface in the header file. That means things like class/struct definitions, function prototypes, and global constants. Then the implementations of said functions are placed in a corresponding source file. When making the module available to the public, you would compile the source files into binaries (often .so or .dll depending on operating system) and deliver the .h files along with the .so files. As a user of one of these libraries you need to do two things to properly use it: include the header file in your source (e.g. #include ) when compiling your code, tell the linker where to find the implementation binaries that correspond to the headers. (e.g. gcc myCoolFile.c -l myCoolOtherFile). Side note: when you install a library it typically places the .h and .so files in directories where your compiler will look for them by default (on Linux, somewhere like /usr/include and /usr/lib/) so providing just the name of the library is usually sufficient for both of the above steps. One of the benefits to packaging libraries in this way is to save compilation time. Imagine having to include the source code (.c files) for every library you used (stdio.h, stdlib.h, etc) in every project. You would need to compile each of those every time and each is potentially a massive amount of code. By using libraries as pre-compiled binaries and headers you get to save a lot of compilation time. Answer from rollingthunder29 on reddit.com
🌐
GeeksforGeeks
geeksforgeeks.org › c language › header-files-in-c-cpp-and-its-uses
Header Files in C - GeeksforGeeks
September 13, 2025 - In C programming, a header file is a file that ends with the .h extension and contains features like functions, data types, macros, etc that can be used by any other C program by including that particular header file using "#include" preprocessor.
🌐
Readthedocs
utat-ss.readthedocs.io › en › master › c-programming › c_h_files.html
.c and .h Files — UTAT Space Systems Documentation 1.0 documentation
.c files contain the implementation of the code, while .h files exist to provide interfaces that allow a file to access functions, global variables, and macros from other files.
🌐
TutorialsPoint
tutorialspoint.com › home › cprogramming › c header files
Header Files in C
June 10, 2012 - The #include preprocessor directive is used to make the definitions of functions, constants and macros etc. from one file, usually called as a header file, available for use in another C code.
🌐
GNU
gcc.gnu.org › onlinedocs › cpp › Header-Files.html
Header Files (The C Preprocessor)
If they need to be changed, they can be changed in one place, and programs that include the header file will automatically use the new version when next recompiled. The header file eliminates the labor of finding and changing all the copies as well as the risk that a failure to find one copy ...
🌐
Medium
medium.com › @Dev_Frank › libraries-in-c-b989c5d66ba2
Header Files In C. A header file in C is a file with a .h… | by Dev Frank | Medium
July 19, 2024 - A header file in C is a file with a .h extension that contains C declarations and macro definitions to be shared between several source…
🌐
FileFormat.com
docs.fileformat.com › programming › h
H - C/C++ Header File Format
May 7, 2021 - A file saved with h file extension is a header file used in C/C++ files to include the declaration of variables, constants, and functions. These are referred by the C++ implementation files that contain the actual implementation of these functions. A .h header file can also include additional ...
🌐
Reddit
reddit.com › r/learnprogramming › how do header files work in c/c++?
r/learnprogramming on Reddit: How Do Header Files Work in C/C++?
November 27, 2022 -

I want to understand header files better. Both C and C++ are not languages I work with commonly but I am interested in them.

If I have a file called myCoolFile.c and I want to call a function from my other file myOtherCoolerFile.c, how does that exactly work?

My understanding is that I use a header file but if I declare the function in that file then I run the issue of it not having a body. It only has a declaration.

I could define the function body in the header file but then I might as well have just included myOtherCoolerFile.c instead.

Sorry for the newbie question. I am not overly familiar with C/C++.

Top answer
1 of 9
9
The convention in C/C++ when implementing a module/library is to divide it into headers (.h) and source files (.c, .cc, .cpp). You typically include only the bare minimum interface in the header file. That means things like class/struct definitions, function prototypes, and global constants. Then the implementations of said functions are placed in a corresponding source file. When making the module available to the public, you would compile the source files into binaries (often .so or .dll depending on operating system) and deliver the .h files along with the .so files. As a user of one of these libraries you need to do two things to properly use it: include the header file in your source (e.g. #include ) when compiling your code, tell the linker where to find the implementation binaries that correspond to the headers. (e.g. gcc myCoolFile.c -l myCoolOtherFile). Side note: when you install a library it typically places the .h and .so files in directories where your compiler will look for them by default (on Linux, somewhere like /usr/include and /usr/lib/) so providing just the name of the library is usually sufficient for both of the above steps. One of the benefits to packaging libraries in this way is to save compilation time. Imagine having to include the source code (.c files) for every library you used (stdio.h, stdlib.h, etc) in every project. You would need to compile each of those every time and each is potentially a massive amount of code. By using libraries as pre-compiled binaries and headers you get to save a lot of compilation time.
2 of 9
2
Technically they dont work with C or C++ (but that is being overly pedantic). They work with the C preprocessor. Think of #include as "copy that file's content into this one." You can see the preprocessor output with the right compiler flags https://stackoverflow.com/questions/3742822/preprocessor-output
Top answer
1 of 12
154

Header files (.h) are designed to provide the information that will be needed in multiple files. Things like class declarations, function prototypes, and enumerations typically go in header files. In a word, "declarations".

Code files (.cpp) are designed to provide the implementation information that only needs to be known in one file. In general, function bodies, and internal variables that should/will never be accessed by other modules, are what belong in .cpp files. In a word, "definitions".

The simplest question to ask yourself to determine what belongs where is "if I change this, will I have to change code in other files to make things compile again?" If the answer is "yes" it probably belongs in the header file; if the answer is "no" it probably belongs in the code file.

2 of 12
67

Fact is, in C++, this is somewhat more complicated that the C header/source organization.

What does the compiler see?

The compiler sees one big source (.cpp) file with its headers properly included. The source file is the compilation unit that will be compiled into an object file.

So, why are headers necessary?

Because one compilation unit could need information about an implementation in another compilation unit. So one can write for example the implementation of a function in one source, and write the declaration of this function in another source needing to use it.

In this case, there are two copies of the same information. Which is evil...

The solution is to share some details. While the implementation should remain in the Source, the declaration of shared symbols, like functions, or definition of structures, classes, enums, etc., could need to be shared.

Headers are used to put those shared details.

Move to the header the declarations of what need to be shared between multiple sources

Nothing more?

In C++, there are some other things that could be put in the header because, they need, too, be shared:

  • inline code
  • templates
  • constants (usually those you want to use inside switches...)

Move to the header EVERYTHING what need to be shared, including shared implementations

Does it then mean that there could be sources inside the headers?

Yes. In fact, there are a lot of different things that could be inside a "header" (i.e. shared between sources).

  • Forward declarations
  • declarations/definition of functions/structs/classes/templates
  • implementation of inline and templated code

It becomes complicated, and in some cases (circular dependencies between symbols), impossible to keep it in one header.

Headers can be broken down into three parts

This means that, in an extreme case, you could have:

  • a forward declaration header
  • a declaration/definition header
  • an implementation header
  • an implementation source

Let's imagine we have a templated MyObject. We could have:

// - - - - MyObject_forward.hpp - - - - 
// This header is included by the code which need to know MyObject
// does exist, but nothing more.
template<typename T>
class MyObject ;

.

// - - - - MyObject_declaration.hpp - - - - 
// This header is included by the code which need to know how
// MyObject is defined, but nothing more.
#include <MyObject_forward.hpp>

template<typename T>
class MyObject
{
   public :
      MyObject() ;
   // Etc.
} ;

void doSomething() ;

.

// - - - - MyObject_implementation.hpp - - - - 
// This header is included by the code which need to see
// the implementation of the methods/functions of MyObject,
// but nothing more.
#include <MyObject_declaration.hpp>

template<typename T>
MyObject<T>::MyObject()
{
   doSomething() ;
}

// etc.

.

// - - - - MyObject_source.cpp - - - - 
// This source will have implementation that does not need to
// be shared, which, for templated code, usually means nothing...
#include <MyObject_implementation.hpp>

void doSomething()
{
   // etc.
} ;

// etc.

Wow!

In the "real life", it is usually less complicated. Most code will have only a simple header/source organisation, with some inlined code in the source.

But in other cases (templated objects knowing each others), I had to have for each object separate declaration and implementation headers, with an empty source including those headers just to help me see some compilation errors.

Another reason to break down headers into separate headers could be to speed up the compilation, limiting the quantity of symbols parsed to the strict necessary, and avoiding unecessary recompilation of a source who cares only for the forward declaration when an inline method implementation changed.

Conclusion

You should make your code organization both as simple as possible, and as modular as possible. Put as much as possible in the source file. Only expose in headers what needs to be shared.

But the day you'll have circular dependancies between templated objects, don't be surprised if your code organization becomes somewhat more "interesting" that the plain header/source organization...

^_^

Find elsewhere
Top answer
1 of 6
154

.c : c file (where the real action is, in general)

.h : header file (to be included with a preprocessor #include directive). Contains stuff that is normally deemed to be shared with other parts of your code, like function prototypes, #define'd stuff, extern declaration for global variables (oh, the horror) and the like.

Technically, you could put everything in a single file. A whole C program. million of lines. But we humans tend to organize things. So you create different C files, each one containing particular functions. That's all nice and clean. Then suddenly you realize that a declaration you have into a given C file should exist also in another C file. So you would duplicate them. The best is therefore to extract the declaration and put it into a common file, which is the .h

For example, in the cs50.h you find what are called "forward declarations" of your functions. A forward declaration is a quick way to tell the compiler how a function should be called (e.g. what input params) and what it returns, so it can perform proper checking (for example if you call a function with the wrong number of parameters, it will complain).

Another example. Suppose you write a .c file containing a function performing regular expression matching. You want your function to accept the regular expression, the string to match, and a parameter that tells if the comparison has to be case insensitive.

in the .c you will therefore put

bool matches(string regexp, string s, int flags) { the code }

Now, assume you want to pass the following flags:

0: if the search is case sensitive

1: if the search is case insensitive

And you want to keep yourself open to new flags, so you did not put a boolean. playing with numbers is hard, so you define useful names for these flags

#define MATCH_CASE_SENSITIVE 0
#define MATCH_CASE_INSENSITIVE 1

This info goes into the .h, because if any program wants to use these labels, it has no way of knowing them unless you include the info. Of course you can put them in the .c, but then you would have to include the .c code (whole!) which is a waste of time and a source of trouble.

2 of 6
26

Of course, there is nothing that says the extension of a header file must be .h and the extension of a C source file must be .c. These are useful conventions.

E:\Temp> type my.interface
#ifndef MY_INTERFACE_INCLUDED
#define MYBUFFERSIZE 8
#define MY_INTERFACE_INCLUDED
#endif

E:\Temp> type my.source
#include <stdio.h>

#include "my.interface"

int main(void) {
    char x[MYBUFFERSIZE] = {0};
    x[0] = 'a';
    puts(x);
    return 0;
}

E:\Temp> gcc -x c my.source -o my.exe

E:\Temp> my
a
🌐
Codecademy
codecademy.com › docs › header files
C | Header Files | Codecademy
January 23, 2025 - Header files, typically denoted by a .h suffix, are used to provide forward declarations of functions, data types, macros, and more, but they do not contain the definitions. This prevents multiple definitions across the codebase, adhering to ...
🌐
Blogger
betterembsw.blogspot.com › 2011 › 08 › proper-use-of-h-and-c-files.html
Better Embedded System SW: Proper use of .h and .c files
The .h file is primarily for use by *other* .c files to get the interface information.ReplyDelete ... " Variables are created/declared/initialized inside the .c file, and the .c file generally does not include its own .h file. The .h file is primarily for use by *other* .c files to get the ...
🌐
Bugsee
bugsee.com › home › understanding the .h file extension
Understanding the .h File Extension – Modular Programming
November 26, 2025 - A .h file, short for header file, is a companion to your source files in languages like C, C++, and Objective-C. It stores declarations that can be shared across multiple parts of a program.
🌐
Old Dominion University
cs.odu.edu › ~zeil › cs333 › f13 › Public › faq › faq-htmlsu21.html
What goes in a .h file? What goes in a .cpp file?
A .h file is intended to be #included from many different .cpp files that make up a single program. In fact, the earliest stage of compilation, the preprocessor, actually replaces each #include by the full contents of the included file.
🌐
File Info
fileinfo.com › extension › h
H File - What is an .h file and how do I open it?
June 17, 2020 - An H file is a header file referenced by a document written in C, C++, or Objective-C source code. It may contain variables, constants, and functions that are used by other files within a programming project.
🌐
Cornell Computer Science
cs.cornell.edu › courses › cs3410 › 2024fa › rsrc › c › header.html
Prototypes & Headers - CS 3410
Even though the C language makes no formal distinction between what you can do in headers and in other files, it is a universal convention that headers have the .h filename extension while “implementation” files use the .c extension.
🌐
Oracle
docs.oracle.com › cd › E19462-01 › 819-4676 › gbbnn › index.html
C Header Files (Sun Java System Access Manager 7.1 C API Reference)
The name of a header file, by convention, ends with the .h extension. It is inserted inside a program by coding the #include preprocessor directive. By default, Access Manager C header files are installed in the /AccessManager-base/SUNWam/include directory.
Top answer
1 of 4
10
  1. In short;

    • The header file defines the API for a module. It's a contract listing which methods a third party can call. The module can be considered a black box to third parties.

    • The implementation implements the module. It is the inside of the black box. As a developer of a module you have to write this, but as a user of a third party module you shouldn't need to know anything about the implementation. The header should contain all the information you need.

  2. Some parts of a header file could be auto generated - the method declarations. This would require you to annotate the implementation as there are likely to be private methods in the implementation which don't form part of the API and don't belong in the header.

Header files sometimes have other information in them; type definitions, constant definitions etc. These belong in the header file, and not in the implementation.

2 of 4
5

The main reason for a header is to be able to #include it in some other file, so you can use the functions in one file from that other file. The header includes (only) enough to be able to use the functions, not the functions themselves, so (we hope) compiling it is considerably faster.

Maintaining the two separately most results from nobody ever having written an editor that automates the process very well. There's not really a lot of reason they couldn't do so, and a few have even tried to -- but the editors that have done so have never done very well in the market, and the more mainstream editors haven't adopted it.

🌐
Cprogramming
cboard.cprogramming.com › c-programming › 60805-difference-between-h-c-files.html
Difference between .h and .c files
They typically contain the source code implementation of the functions that were prototyped in the appropriate header file. "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god.
🌐
Quora
quora.com › What-are-the-h-files-in-C-language
What are the .h files in C language? - Quora
Answer (1 of 8): These files are header files. There are several default header files in the C compilers which include many function declarations, macros etc. Suppose, you want to print a variable by using printf() function.