Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to structure a C program effectively

Tags:

c

People also ask

How do you write the structure of C program?

A 'C' program is divided into six sections: Documentation, Link, Definition, Global Declaration, Main() Function, Subprograms. While the main section is compulsory, the rest are optional in the structure of the C program.

What is the best way to practice C programming?

Begin with solving basic level logical questions, and while solving always follow the best approach and best practices. Show activity on this post. A really good way to exercise C is to read (or reread) The C Programming Language (K&R) and to do every exercise at the end of each chapter.

What is an efficient program in C?

Description. Efficient C/C++ Programming describes a practical, real-world approach to efficient C/C++ programming. Topics covered range from how to save storage using a restricted character set and how to speed up access to records by employing hash coding and caching.


This is quite a normal and sensible practice. But try not to expose the struct layout in header files, so that you have some flexibility in how it's implemented and manage your dependencies better.

See Opaque pointer for more details.


What you are suggesting is the way I always wrote C programs back in the days when I did such a thing. I don't think it is "poor mans OO", I think it is sensible procedural programming practice.

I would observe a couple of things about your C code:

  • use typedefs with struct definitions so you don't need to scatter the 'struct' keyword throughout the code
  • only use casts when they are actually needed - the cast on the return value from malloc() is unecessary

Hmmm... We used to just use naming conventions... Ergo: str* does stuff with what common datastructure? So maybe just take the C# syntax and s/./_/g?

  • foo_constructor
  • foo_destructor
  • foo_someMethod
  • foo_someMethod2 // ain't no overloading in ANSI C
  • foo_otherMethod

... and there ain't no inheritance ...

  • foo2_constructor
  • foo2_destructor
  • foo2_someMethod // and there ain't no polymorphism

But look on the bright side... you can use pointer-to-pointer-to-pointer-to-function-returning-a-pointer-to-pointer-int! Oh the joy!

My bestest advise is to learn the lessons of Java (and by inference C#) and structure your libraries to NOT have side-effects... more typdefs == less headaches... and if your work-out how to follow this sage advise please let me know ;-)

Cheers. Keith.


That's a pretty reasonable way to write a C program. There is another large application out there, which does pretty much the same stuff - called the Linux kernel. Some nearly OO-features used in there:

  • structs and operations on structs for encapsulation just like in your example
  • pointers to base structs as a form of poor man's inheritance -- you'll find loads of references to struct kobject in there
  • macros to generate functions as a replacement for template programming