9

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.

csss
  • 1,937
  • 2
  • 14
  • 24

5 Answers5

7

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.

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
Neil Townsend
  • 6,024
  • 5
  • 35
  • 52
4

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

Community
  • 1
  • 1
MOHAMED
  • 41,599
  • 58
  • 163
  • 268
  • Is this the standard practice for writing C applications though? I don't want to perform some of kind of hacks so that I can 'sort of use objects in C'. I want to develop C applications in the standard best practice style. Is what you recommend the standard way of doing things in C? – csss Mar 25 '13 at 09:09
  • @csss although there is no standard recommended procedures in programming in C, the art has been evolved through time. The book I mentioned "C Interfaces and Implementations" teach you how to do just that. – Aniket Inge Mar 25 '13 at 09:10
  • the link above and even this link http://stackoverflow.com/questions/351733/can-you-write-object-oriented-code-in-c propose a method to design a c application which is close to the oriented project design it's not a hack. otherwise you can use c++ instead – MOHAMED Mar 25 '13 at 09:11
  • The first answer on this page says that doing OOP with C is 'tedious and not completely advisable' - http://stackoverflow.com/questions/8180466/using-uml-for-c-programming If it is 'not advisable' I wouldn't consider it best practice. So is the author of that post wrong? – csss Mar 25 '13 at 09:15
  • 1
    @csss if you want to do what C++ already does(and did in the past) that is do object oriented programming in C in C++ style then it is wrong. However you can program C like an object BASED programming language and solve a lot of problems. Code duplication is never an issue - why would it be? All good C programs are programmed with functions in mind, one function one job. 2 functions 2 jobs.. – Aniket Inge Mar 25 '13 at 09:17
2

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

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
  • Haven't read the book, but has taken a look over examples. Generally it looks good, but I think using defines to substitute type is a bit questionable paradigm. – Valeri Atamaniouk Mar 25 '13 at 09:13
2

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.
nvseenu
  • 125
  • 6
1

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.

Community
  • 1
  • 1
Pieter Kuijpers
  • 7,247
  • 5
  • 28
  • 36