Correctly Using Header Files?


Lately I have been using header files to split up my program into separate files, (C files containing functions and header files declaring them). Every thing works fine but for some reason, I need to include <stdio.h> and <stdlib.h> in EVERY C file... or my project fails to compile. Is this expected behavior?


C modules need to know either how something is defined, or where it can find a definition. If the definition is in the header file, then you should include it in the modules that use it. Here is a <a href="http://gcc.gnu.org/onlinedocs/cpp/Header-Files.html" rel="nofollow">link</a> to information regarding header files.


The answer would depend on whether or not that functions might depend on other declared functions in other .c/.h files.

For example:


#include "filea.h"; methodA() { methodB(); }


#include <somelibrary.h> #include "fileb.h" methodB(); { somelibrarycode(); }

This will not compile unless filea.c includes the header for fileb.h as it has some external dependency that is not resolved.

If this is not what you're describing than there is some other spaghettification happening, or you accidentally statically typed functions preventing them from being seen outside of the .c file.

One possible solution to this problem is to have a single shared.h with all the other includes, but I personally don't recommend this as this merely masks the issue instead of making it readily apparently which files depend on what and establish clear lines of dependency.


They must be included <em>some</em> way.

Some projects require long list of includes in .c files, possibly with mandatory sort, even forcing assumption that no header includes any other header.

Some allow assuming some includes form some headers.

Some use collection headers (that include a list of small headers) and replace long lists with those.

Some go even further, using "forced header" option of compiler, so include will not appear anywhere, and declare the content to be implicitly assumed. It may go on project or whole codebase level, or combined. It plays pretty well with precompiled headers.

(And there are many more strategies, you get the figure, all with some pros&cons.)


  • ObjectiveC:Is possible to set global button(not in the class) to activate global method directly?
  • Update a list every time a function within a class is executexecuted with the function arguments and
  • Passing and using Class in a method in Java
  • How to compare one file with bunch of files in linux
  • Cannot access class methods via generics
  • Calling a method of view in emberjs
  • Override method from instance of the class
  • Using awk how do I print all lines containing duplicates of specific columns?
  • Extract a column from a string in Python
  • Global variables in CMake for dependency tracking
  • Compare 2 files in Python
  • Running sonar-scanner from setup.py
  • Assign file names to a variable in shell
  • Spring security- Method level security doesn't work on calling from another method
  • How to point to first element when object.next reached the end?
  • Create a 7z Archive for files created by date using Powershell
  • Sending Special characters as a message;
  • Mixing PDO and MySQL Functions?
  • Merge Command in R
  • window.onbeforeunload in javascript
  • MonadTransControl instance for ProxyFast/ProxyCorrect
  • How to add regEx in angular filter
  • Msvcr71.dll Msvcp71.dll missing
  • JSON - slashes not escaping
  • Linking Ghostscript to pypdfocr in Windows Platform
  • Unable to click on the next page button containing “>” sign
  • Android custom URI scheme incorrectly encoded when type in browser
  • Flask not finding files in my package's 'static' directory
  • Jquery Mobile pageLoading() Method how does it work?
  • CodeIgniter URI Parameter is partially bypassing an “if” statement
  • Dart - Isolate Cross Window Communication
  • How to autopopulate a field in SugarCRM form
  • How to view images from protected folder with php?
  • Webgrid not refreshing after delete MVC
  • Jquery UI tool tip close icon
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • Web-crawler for facebook in python
  • Traverse Array and Display in markup
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • Qt: Run a script BEFORE make