今日已更新 248 条资讯 | 累计 38105 条内容
关于我们

Program Organization (with Examples in C)

Paul J. Lucas 2026年09月01日 08:54 0 次阅读 来源:Dev.to

Introduction In my book Why Learn C , Chapter 13, Program Organization , I wrote in part: For all but the most trivial programs, a typical C program is composed of several source ( .c ) files and header ( .h ) files. Often, .c and .h files come in pairs where the .c implements some functionality and the .h provides the “public” API for using it. All the functions comprising a program are spread among pairs of files with each pair specializing in some particular aspect of the program. One or more pairs roughly approximates a “module” in other languages. I also gave an example using the source files of ad : ad.c color.h match.c options.h unicode.c util.h ad.h dump.c match.h pjl_config.h unicode.h color.c dump_c.c options.c reverse.c util.c For example, color.c contains the definitions of functions for printing text in color to a terminal and its corresponding color.h contains their declarations so that other files may #include it to use those functions. The ad example was chosen for the book because it’s a fairly small program where, consequently, the source files and their names make sense once you know what the purpose of ad is. (If you didn’t click the link, ad dumps the contents of any file as ASCII.) One of the things some newcomers to C (or programming in general using any language) struggle with is how to organize source files. In this article, I’m going to flesh out the details of program organization using a mid-sized program, include-tidy (Tidy), as an example. As a preview, here are its source files: array.c file_ext.c path_util.c symbol.h array.h file_ext.h path_util.h toml_lite.c bit_util.c fnv1a.c path_util_test.c toml_lite.h bit_util.h fnv1a.h pjl_config.h toml_test.c clang_util.c hash_table.c print.c trans_unit.c clang_util.h hash_table.h print.h trans_unit.h cli_options.c hash_table_test.c proxies.c type_traits.h cli_options.h include-tidy.c proxies.h typedef.c color.c include-tidy.h red_black.c typedef.h color.h include.c red_black.h unit_test.c conf

本文内容来源于互联网,版权归原作者所有
查看原文