I am planning to develop an application in C. My programming experience has always been with object oriented languages. Hence I always think in terms of classes, interfaces, inheritance, polymorphism, etc, when designing an application.
All the C books I've looked at deal with how to program in C or focus on a particular topic, I couldn't find any that talk about application architecture in C. So how do you structure a C application when the OOP features are not available? How do you keep everything modular and well organized and avoid code duplication (no OOP seems like there will be alot of code duplication)?
Edit: I am not looking for answers on 'how to write OOP code in C'. I am looking for the standard practice way of structuring C applications so they are modular and well organized. If the standard practice way is to hack on some OOP features then that is fair enough but if its not then there is no point in telling me to go down that route.
It is a different way of thinking. The core philosophy of C can be summarised as:
data + algorithms = programs
So to design an application in C:
You need to think carefully about what the data is, and define struct
s which reflect that well, and facilitate the relationships between different views on the data.
You need to think about what algorhythms are going to operate on what data, and what data they produce. This helps to clarify the struct
s you should have, and help to show what should be blocked together to create reusable blocks of code.
One way of moving to this approach from an OOP approach is to imagine that one struct
+ one .c file = a class, and to put in the .h file the struct definition and the externally accessible functions (public methods).
You have to write a lot of code to do boring things like memory allocation and freeing and all that jazz. It's not as bad as it sounds, but factor this into your design.
you can design your C project as oriented object project and then replace the class by structure. this was recommended to me in this topic and in this topic
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With