mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 09:41:36 +00:00
Merge pull request #1427 from zfergus2/master
[c/en] Added a section for header files.
This commit is contained in:
commit
66bc42e31b
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user