Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Structuring C applications?

Tags:

c

architecture

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.

like image 915
csss Avatar asked Mar 25 '13 08:03

csss


2 Answers

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:

  1. You need to think carefully about what the data is, and define structs which reflect that well, and facilitate the relationships between different views on the data.

  2. You need to think about what algorhythms are going to operate on what data, and what data they produce. This helps to clarify the structs you should have, and help to show what should be blocked together to create reusable blocks of code.

  3. 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).

  4. 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.

like image 102
Neil Townsend Avatar answered Oct 26 '22 09:10

Neil Townsend


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

like image 4
MOHAMED Avatar answered Oct 26 '22 09:10

MOHAMED