summaryrefslogtreecommitdiffhomepage
path: root/c.html.markdown
diff options
context:
space:
mode:
authorZachary Ferguson <zfergus2@users.noreply.github.com>2015-10-09 11:52:08 -0400
committerZachary Ferguson <zfergus2@users.noreply.github.com>2015-10-09 11:52:08 -0400
commite418849d7696bd91a144ccdff451025899ac10e0 (patch)
treee59e82c84f8ba57a3ccd43b5f5ddb46c1e6184fd /c.html.markdown
parent7aca9100a042f3813d383f979f8c32a95ecc4bbb (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.markdown50
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)