C MoreLesson 52Free preview
C Organize Code
This lesson is part of C · All levels
Summary
The neat C project separates the code into many .c and .h files, where the header contains declarations such as prototypes, types, and constants, while the source file contains definitions. Each file is compiled separately and then linked to a single program, so that small changes do not trigger the entire project to be recompiled. Include guard with #ifndef or #pragma once to prevent header insertion more than once.
Key points
- Patterns include guard: HEADER_H, HEADER_H, header content, and then #endif. (#ifndef)
- Header for declaration and file .c for definition; internal module functions are given static to keep them invisible from the outside.
- Extern declares a variable or function defined in another file.
- Group the code per module, for example util.h for a common function and data.h for a common data type.
- Compiler separately with gcc -c and then link, or use Makefile to automate. (modul.c)
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.