tags:

views:

372

answers:

4

I've only had to code in C a few times, and it seems like every time I do, it becomes some unmanageable beast.

I've done most of my programming in C# and .Net so I am very accustomed to the class style architecture, but I can't seem to grasp organization in C applications. Should I put functions that are related in a certain file, put function definitions in one file and processing in another, etc. Are there any good resources out there on this subject?

See also: http://stackoverflow.com/questions/674722/struggling-with-c-coming-from-object-oriented-land

+3  A: 

C Interfaces and Implementations by David Hanson explains a nice usable organization scheme in the first chapter (if I recall correctly) and shows it in use in the remaining ones developing substantial libraries.

Darius Bacon
+2  A: 

the old "structured programming" way of coding C programs is to modularise them into files. Instead of having code in objects, make sure you put all related code into individual files.

If you think of a file as a poor man's object, you'll be mostly there to organising your C apps like you do with your C# ones.

gbjbaanb
+5  A: 

Here is an analogy that might help you:

A class is a data structure of some kind and a set of functions to access and manipulate that data. The c language doesn't provide any syntatic sugar to organize this relationship for you, but you can still build your code around this idea. So, just do it: design you project around a bunch of "classes".

Code organization generally takes on one of two forms:

  • Each function gets one header (interface) file with a .h extension and one implementation file with a .c extension
  • Each "class" (i.e. set of related functions) gets one header file and one implementation file.

So, you already know how to do this...

dmckee
+2  A: 

Since have experience in C#, you may be interested in the responses to my SO question: "Object-Orientation in C". If they interest you, it may be useful to take a look at the responses to the related question: "Can you write object oriented code in c?". If you plan to use a scheme like any of the ones mentioned in the responses to the aforementioned questions, please keep in mind that the most important thing is your design, not the syntactic sugar which may/may not make it look pretty.

Anthony Cuozzo