diff options
author | Zachary Ferguson <zfergus2@users.noreply.github.com> | 2015-10-07 23:45:01 -0400 |
---|---|---|
committer | Zachary Ferguson <zfergus2@users.noreply.github.com> | 2015-10-07 23:45:01 -0400 |
commit | e1ac6209a8d3f43e7a018d79454fb1095b3314c0 (patch) | |
tree | 65676fe14c7437234eb28642a343eabf3823efed /c.html.markdown | |
parent | cea7db46eb1365c42d2e56a84af0ee2e3a750569 (diff) |
[c/en] Added a section for header files.
Added a section for header files.
Included a discussion of what belongs in a header file and what does
not.
Diffstat (limited to 'c.html.markdown')
-rw-r--r-- | c.html.markdown | 50 |
1 files changed, 49 insertions, 1 deletions
diff --git a/c.html.markdown b/c.html.markdown index db2ac930..f1201eac 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -6,7 +6,7 @@ contributors: - ["Árpád Goretity", "http://twitter.com/H2CO3_iOS"] - ["Jakub Trzebiatowski", "http://cbs.stgn.pl"] - ["Marco Scannadinari", "https://marcoms.github.io"] - + - ["Zachary Ferguson", "https://github.io/zfergus2"] --- Ah, C. Still **the** language of modern high-performance computing. @@ -630,6 +630,54 @@ typedef void (*my_fnp_type)(char *); ``` +Header files are an important part of c as they allow for the connection of c +source files and can simplify code and definitions by seperating them into +seperate files. + +Header files are syntaxtically similar to c source files but reside in ".h" +files. They can be included in your c source file by using the precompiler +command #include "example.h", given that example.h exists in the same directory +as the c file. + +```c +/* A safe guard to prevent the header from being defined too many times. This */ +/* happens in the case of circle dependency, the contents of the header is */ +/* already defined. */ +#ifndef EXAMPLE_H /* if EXAMPLE_H is not yet defined. */ +#define EXAMPLE_H /* Define the macro EXAMPLE_H. */ + +/* Other headers can be included in headers and therefore transitively */ +/* included into files that include this header. */ +#include <string.h> + +/* Like c source files macros can be defined in headers and used in files */ +/* that include this header file. */ +#define EXAMPLE_NAME "Dennis Ritchie" +/* Function macros can also be defined. */ +#define ADD(a, b) (a + b) + +/* Structs and typedefs can be used for consistency between files. */ +typedef struct node +{ + int val; + struct node *next; +} Node; + +/* So can enumerations. */ +enum traffic_light_state {GREEN, YELLOW, RED}; + +/* Function prototypes can also be defined here for use in multiple files, */ +/* but it is bad practice to define the function in the header. Definitions */ +/* should instead be put in a c file. */ +Node createLinkedList(int *vals, int len); + +/* Beyond the above elements, other definitions should be left to a c source */ +/* file. Excessive includeds or definitions should, also not be contained in */ +/* a header file but instead put into separate headers or a c file. */ + +#endif /* End of the if precompiler directive. */ + +``` ## Further Reading Best to find yourself a copy of [K&R, aka "The C Programming Language"](https://en.wikipedia.org/wiki/The_C_Programming_Language) |