From e418849d7696bd91a144ccdff451025899ac10e0 Mon Sep 17 00:00:00 2001 From: Zachary Ferguson Date: Fri, 9 Oct 2015 11:52:08 -0400 Subject: [PATCH 1/4] [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. --- c.html.markdown | 50 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) 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 + +/* 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) From 79ced08e094e488eb21d026c882babc50d0ca168 Mon Sep 17 00:00:00 2001 From: Zachary Ferguson Date: Tue, 13 Oct 2015 11:05:20 -0400 Subject: [PATCH 2/4] Added Header title + Added a header file title * Changed ```c to ```h --- c.html.markdown | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/c.html.markdown b/c.html.markdown index f1201eac..0c4916ac 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -630,6 +630,8 @@ typedef void (*my_fnp_type)(char *); ``` +### 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. @@ -639,7 +641,7 @@ 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 +```h /* 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. */ From 27bb1a1e80b727b00b5ea8045d4bfa07eb1293bd Mon Sep 17 00:00:00 2001 From: Zachary Ferguson Date: Wed, 14 Oct 2015 21:59:42 -0400 Subject: [PATCH 3/4] Removed separate code section Removed the separate code section for header files --- c.html.markdown | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/c.html.markdown b/c.html.markdown index 0c4916ac..5e8e13c1 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -628,9 +628,7 @@ typedef void (*my_fnp_type)(char *); // , | left to right // //---------------------------------------------------// -``` - -### Header Files +/******************************* 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 @@ -640,8 +638,8 @@ 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. +*/ -```h /* 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. */ From 8417366a1b2d2b7bce35e2e4a4fdccee2a60f457 Mon Sep 17 00:00:00 2001 From: Zachary Ferguson Date: Thu, 15 Oct 2015 14:50:20 -0400 Subject: [PATCH 4/4] Cleaned up c file --- c.html.markdown | 4 ---- 1 file changed, 4 deletions(-) diff --git a/c.html.markdown b/c.html.markdown index 7bb363b3..a8f71057 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -6,12 +6,8 @@ contributors: - ["Árpád Goretity", "http://twitter.com/H2CO3_iOS"] - ["Jakub Trzebiatowski", "http://cbs.stgn.pl"] - ["Marco Scannadinari", "https://marcoms.github.io"] -<<<<<<< HEAD - ["Zachary Ferguson", "https://github.io/zfergus2"] -======= - ["himanshu", "https://github.com/himanshu81494"] - ->>>>>>> refs/remotes/adambard/master --- Ah, C. Still **the** language of modern high-performance computing.