csss
csss

Reputation: 1997

Structuring C applications?

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.

Upvotes: 10

Views: 1477

Answers (5)

Pieter Kuijpers
Pieter Kuijpers

Reputation: 7307

It is possible to practice TDD with C, see C programming and TDD .

If you're used to practicing TDD, you know it will help you keep your code well organized and modular.

Upvotes: 1

nvseenu
nvseenu

Reputation: 125

First you will identify the components and their interactions to solve the problem. then inside each component, below practices can be used.

  1. Design the public functions first.
  2. design the data structure ( i.e struct ) the functions are going to work
  3. Modify the public functions to take the corresponding structure as pointer argument. [ There is no instance variable concept in c. you need to define a structure and pass structure between functions ] .
  4. group the functions with related data structure in a header file.
  5. provide the implementations to the public functions in a separate c file which includes the header file you defined.
  6. make all your private/helper methods as static, so they will not be visible to other c files.
  7. Since there is no namespace concept in C, Ensure your public functions are not conflicted with existing library functions. some people are using name mangling like {short name of header file}_{function name}
  8. allocating and release the memory is the developers responsibility. it is better to have initialize and free functions to allocate and clear the memory along with the public functions designed.
  9. Follow the coding styles you are comfortable with.
  10. Design each components as shared library , so that you don't need to compile them every time.

Upvotes: 2

Neil Townsend
Neil Townsend

Reputation: 6084

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.

Upvotes: 7

Aniket Inge
Aniket Inge

Reputation: 25705

Also, to create re-usable C software, read this book by David R. Hanson

https://sites.google.com/site/cinterfacesimplementations/

Basic OOP is best done with the techniques mentioned in Alex Schriner's OOC.pdf book

Upvotes: 2

MOHAMED
MOHAMED

Reputation: 43518

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

Upvotes: 4

Related Questions