<p>I'm used to doing all my coding in one C file. However, I'm working on a project large enough that it becomes impractical to do so. I've been #including them together but I've run into cases where I'm #including some files multiple times, etc. I've heard of .h files, but I'm not sure what their function is (or why having 2 files is better than 1).</p>

<p>What strategies should I use for organizing my code? Is it possible to separate "public" functions from "private" ones for a particular file?</p>

<p><a href="https://stackoverflow.com/questions/47878/codereview-tiny-encryption-algorithm-for-arbitrary-sized-data">This</a> question precipitated my inquiry. The tea.h file makes no reference to the tea.c file. Does the compiler "know" that every .h file has a corresponding .c file?</p>

[{'answer_id': 47928, 'author': 'Andrew', 'author_id': 826, 'author_profile': 'https://Stackoverflow.com/users/826', 'pm_score': 3, 'selected': False, 'text': '<p>Try to make each .c focus on a particular area of functionality. Use the corresponding .h file to declare those functions.</p>\n\n<p>Each .h file should have a \'header\' guard around it\'s content. For example:</p>\n\n<pre><code>#ifndef ACCOUNTS_H\n#define ACCOUNTS_H\n....\n#endif\n</code></pre>\n\n<p>That way you can include "accounts.h" as many times as you want, and the first time it\'s seen in a particular compilation unit will be the only one that actually pulls in its content.</p>\n'}, {'answer_id': 47939, 'author': 'denis phillips', 'author_id': 748, 'author_profile': 'https://Stackoverflow.com/users/748', 'pm_score': 3, 'selected': False, 'text': '<p>A couple of simple rules to start:</p>\n<ol>\n<li>Put those declarations that you want to make &quot;public&quot; into the header file for the C implementation file you are creating.</li>\n<li>Only #include header files in the C file that are needed to implement the C file.</li>\n<li><h1>include header files in a header file only if required for the declarations within that header file.</h1>\n</li>\n<li>Use the include guard method described by Andrew OR use <strong>#pragma once</strong> if the compiler supports it (which does the same thing -- sometimes more efficiently)</li>\n</ol>\n'}, {'answer_id': 47943, 'author': 'Adam Davis', 'author_id': 2915, 'author_profile': 'https://Stackoverflow.com/users/2915', 'pm_score': 3, 'selected': False, 'text': '<h2>Compiler</h2>\n\n<p>You can see an example of a C \'module\' at <a href="https://stackoverflow.com/questions/47878/codereview-tiny-encryption-algorithm-for-arbitrary-sized-data">this topic</a> - Note that there are two files - the header tea.h, and the code tea.c.  You declare all the public defines, variables, and function prototypes that you want other programs to access in the header.  In your main project you\'ll #include  and that code can now access the functions and variables of the tea module that are mentioned in the header.</p>\n\n<p>It gets a little more complex after that.  If you\'re using Visual Studio and many other IDEs that manage your build for you, then ignore this part - they take care of compiling and linking objects.</p>\n\n<h2>Linker</h2>\n\n<p>When you compile two separate C files the compiler produces individual object files - so main.c becomes main.o, and tea.c becomes tea.o.  The linker\'s job is to look at all the object files (your main.o and tea.o), and match up the references - so when you call a tea function in main, the linker modifies that call so it actually does call the right function in tea.  The linker produces the executable file.</p>\n\n<p>There is a <a href="http://www.cs.cf.ac.uk/Dave/C/node35.html" rel="nofollow noreferrer">great tutorial</a> that goes into more depth on this subject, including scope and other issue you\'ll run into.</p>\n\n<p>Good luck!</p>\n\n<p>-Adam</p>\n'}, {'answer_id': 47945, 'author': '1800 INFORMATION', 'author_id': 3146, 'author_profile': 'https://Stackoverflow.com/users/3146', 'pm_score': 2, 'selected': False, 'text': '<p>Your question makes it clear that you haven\'t really done much serious development. The usual case is that your code will generally be far too large to fit into one file. A good rule is that you should split the functionality into logical units (.c files) and each file should contain no more than what you can easily hold in your head at one time.</p>\n\n<p>A given software product then generally includes the output from many different .c files. How this is normally done is that the compiler produces a number of object files (in unix systems ".o" files, VC generates .obj files). It is the purpose of the "linker" to compose these object files into the output (either a shared library or executable).</p>\n\n<p>Generally your implementation (.c) files contain actual executable code, while the header files (.h) have the declarations of the public functions in those implementation files. You can quite easily have more header files than there are implementation files, and sometimes header files can contain inline code as well.</p>\n\n<p>It is generally quite unusual for implementation files to include each other. A good practice is to ensure that each implementation file separates its concerns from the other files.</p>\n\n<p>I would recommend you download and look at the source for the linux kernel. It is quite massive for a C program, but well organised into separate areas of functionality.</p>\n'}, {'answer_id': 47948, 'author': 'cschol', 'author_id': 2386, 'author_profile': 'https://Stackoverflow.com/users/2386', 'pm_score': 6, 'selected': True, 'text': '<p>You should regard .h files as <strong>interface files</strong> of your .c file. Every .c file represents a module with a certain amount of functionality. If functions in a .c file are used by other modules (i.e. other .c files) put the function prototype in the .h interface file. By including the interface file in your original modules .c file and every other .c file you  need the function in, you make this function available to other modules. </p>\n\n<p>If you only need a function in a certain .c file (not in any other module), declare its scope static. This means it can only be called from within the c file it is defined in. </p>\n\n<p>Same goes for variables that are used across multiple modules. They should go in the header file and there they have to marked with the keyword \'extern\'. Note: For functions the keyword \'extern\' is optional. Functions are always considered \'extern\'.</p>\n\n<p>The inclusion guards in header files help to not include the same header file multiple times.</p>\n\n<p>For example:</p>\n\n<p>Module1.c:</p>\n\n<pre>\n    #include "Module1.h"\n\n    static void MyLocalFunction(void);\n    static unsigned int MyLocalVariable;    \n    unsigned int MyExternVariable;\n\n    void MyExternFunction(void)\n    {\n        MyLocalVariable = 1u;       \n\n        /* Do something */\n\n        MyLocalFunction();\n    }\n\n    static void MyLocalFunction(void)\n    {\n      /* Do something */\n\n      MyExternVariable = 2u;\n    }\n</pre>\n\n<p>Module1.h:</p>\n\n<pre>\n    #ifndef __MODULE1.H\n    #define __MODULE1.H\n\n    extern unsigned int MyExternVariable;\n\n    void MyExternFunction(void);      \n\n    #endif\n</pre>\n\n<p>Module2.c</p>\n\n<pre>\n    #include "Module.1.h"\n\n    static void MyLocalFunction(void);\n\n    static void MyLocalFunction(void)\n    {\n      MyExternVariable = 1u;\n      MyExternFunction();\n    }\n</pre>\n'}, {'answer_id': 47955, 'author': 'smh', 'author_id': 1077, 'author_profile': 'https://Stackoverflow.com/users/1077', 'pm_score': 2, 'selected': False, 'text': '<p>To answer your additional question:</p>\n\n<blockquote>\n  <p><a href="https://stackoverflow.com/questions/47878/codereview-tiny-encryption-algorithm-for-arbitrary-sized-data">This</a>\n  question precipitated my inquiry. The\n  tea.h file makes no reference to the\n  tea.c file. Does the compiler "know"\n  that every .h file has a corresponding\n  .c file?</p>\n</blockquote>\n\n<p>The compiler is not primarily concerned with header files. Each invocation of the compiler compiles a source (.c) file into an object (.o) file. Behind the scenes (i.e. in the <code>make</code> file or project file) a command line equivalent to this is being generated:</p>\n\n<pre><code>compiler --options tea.c\n</code></pre>\n\n<p>The source file <code>#include</code>s all the header files for the resources it references, which is how the compiler finds header files.</p>\n\n<p>(I\'m glossing over some details here. There is a lot to learn about building C projects.)</p>\n'}, {'answer_id': 47961, 'author': 'hoyhoy', 'author_id': 3499, 'author_profile': 'https://Stackoverflow.com/users/3499', 'pm_score': 0, 'selected': False, 'text': "<p>The .h files should be used to define the prototypes for your functions.  This is necessary so you can include the prototypes that you need in your C-file without declaring every function that you need all in one file. </p>\n\n<p>For instance, when you <code>#include &lt;stdio.h&gt;</code>, this provides the prototypes for printf and other IO functions.  The symbols for these functions are normally loaded by the compiler by default.  You can look at the system's .h files under /usr/include if you're interested in the normal idioms involved with these files.  </p>\n\n<p>If you're only writing trivial applications with not many functions, it's not really necessary to modularize everything out into logical groupings of procedures.  However, if you have the need to develop a large system, then you'll need to pay some consideration as to where to define each of your functions.  </p>\n"}, {'answer_id': 48044, 'author': 'David L Morris', 'author_id': 3137, 'author_profile': 'https://Stackoverflow.com/users/3137', 'pm_score': 2, 'selected': False, 'text': "<p>As well as the answers supplied above, one small advantage of splinting up your code into modules (separate files) is that if you have to have any global variables, you can limit their scope to a single module by the use of the key word 'static'.  (You could also apply this to functions).  Note that this use of 'static' is different from its use inside a function.</p>\n"}]
['https://Stackoverflow.com/questions/47919', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/658/']