diff options
author | ven <vendethiel@hotmail.fr> | 2015-10-15 20:51:10 +0200 |
---|---|---|
committer | ven <vendethiel@hotmail.fr> | 2015-10-15 20:51:10 +0200 |
commit | 66bc42e31bf62a1592f9b763e12c0b963b3e7d3d (patch) | |
tree | 4fb6275c2c6355aba82be0434a044f78342052c9 /c.html.markdown | |
parent | 68953bd9d97328b8660dad06edd8acb8ff330ede (diff) | |
parent | 8417366a1b2d2b7bce35e2e4a4fdccee2a60f457 (diff) |
Merge pull request #1427 from zfergus2/master
[c/en] Added a section for header files.
Diffstat (limited to 'c.html.markdown')
-rw-r--r-- | c.html.markdown | 52 |
1 files changed, 50 insertions, 2 deletions
diff --git a/c.html.markdown b/c.html.markdown index bfdf276c..a8f71057 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -6,8 +6,8 @@ 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"] - ["himanshu", "https://github.com/himanshu81494"] - --- Ah, C. Still **the** language of modern high-performance computing. @@ -685,8 +685,56 @@ typedef void (*my_fnp_type)(char *); // , | left to right // //---------------------------------------------------// -``` +/******************************* Header Files ********************************** + +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. +*/ + +/* 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) |