Arranging Source and Header Files (the C Way)

Proper code reuse can be challenging because it requires a specific mind-set. A common mistake that even professional programmers make is putting all their code in the same file like this:

1: #include <stdio.h> // For input/output
#include <stdlib.h> // For EXIT_SUCCESS or EXIT_FAILURE
#include <stdbool.h> // For bool, false, and true
5: typedef​ ​struct​ JSON_Object {
// Lots of lines of code here
// ...
} JSON_Object;
10: // Lots of helper functions, data types, and global variables
int​ json_errno = 0;
bool ​read_boolean​(​FILE​* infile) { ​/* ... */​; ​return​ false; }
char​ *​read_string​(​FILE​* infile) { ​/* ... */​; ​return​ NULL; }
void ...

Get Resourceful Code Reuse now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.