How should I organize a small C project? #208323
🏷️ Discussion TypeBug BodyI'm working on a small C project and I'm wondering how I should organize the source files. Is it better to keep everything in one .c file for a small project, or should I separate things into .c and .h files from the beginning? What would you recommend for a beginner? Guidelines
|
Replies: 2 comments
|
For a small C project, I’d start with one For example: The For example: // calculator.h
#ifndef CALCULATOR_H
#define CALCULATOR_H
int add(int a, int b);
#endif// calculator.c
#include "calculator.h"
int add(int a, int b) {
return a + b;
}Then I wouldn't split everything into separate files just for the sake of it. A good rule for beginners is: start simple, but separate code when a file starts having multiple unrelated responsibilities or when you want to reuse a component. This also makes the project easier to test and maintain as it grows. |
|
💬 Your Product Feedback Has Been Submitted 🎉 Thank you for taking the time to share your insights with us! Your feedback is invaluable as we build a better GitHub experience for all our users. Here's what you can expect moving forward ⏩
Where to look to see what's shipping 👀
What you can do in the meantime 💻
As a member of the GitHub community, your participation is essential. While we can't promise that every suggestion will be implemented, we want to emphasize that your feedback is instrumental in guiding our decisions and priorities. Thank you once again for your contribution to making GitHub even better! We're grateful for your ongoing support and collaboration in shaping the future of our platform. ⭐ |
For a small C project, I’d start with one
.cfile if the project is genuinely tiny, but I’d introduce.hand separate.cfiles as soon as you have distinct responsibilities.For example:
The
.hfiles contain declarations, types, and function prototypes, while the.cfiles contain the implementations.For example:
Then
main.ccan simply include the header and use the function.I wouldn'…