Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to structure your c program?

Tags:

c

I've been working/coding in C#/Java for some years, so the basics etc. don't give me to much a hard time. But I've never done anything larger than small commandline learning programs in c.

Now I'm trying to make a mobile phone emulator for Linux and I have no clue how to structure my code when its not object oriented. I have 3 big books that cover c in detail but none of them cover how to code for maintainability in a bigger project.

So i was hoping some of you more experienced people could point me to a best practice or similar?

like image 368
Bernt Avatar asked Oct 13 '09 15:10

Bernt


People also ask

How can you write the basic structure of C program explain with example?

A declaration part and an Execution part. The declaration part is the part where all the variables are declared. The execution part begins with the curly brackets and ends with the curly close bracket. Both the declaration and execution part are inside the curly braces.

What is structure of a program?

The procedural or linear structure of a program is defined by its control flow. More formally, the procedural structure is built from blocks of code, where each block or unit has a single entry and a single exit point in the control flow.


2 Answers

Some thoughts (and this question should be a community wiki)

  • Even if it's not fully-fledged object-oriented programming, try to stick to information hiding practices. Let your functions return pointers or handles to opaque structures (the unix file handle api is a good (early) example. Use static functions where you'd otherwise use private methods.
  • Try to keep related functionality contained to a single file. This will make it easier to use the abovementioned static keyword, as well as give you a good indication when it's time to split off some functionality into a seperate file. For a Java programmer, this shouldn't be too strange a practice.
  • The standard practices on commenting apply. Use something like Doxygen if you're looking for something similar to javadoc/C# XML comments.
  • If you really haven't done anything serious in C for a while, coming to grips with practices for keeping your memory management clean and sensible, even though it's not hard per se, is going to hurt a lot more then establishing practices for maintainability. Just a warning.
like image 107
Michiel Buddingh Avatar answered Sep 28 '22 18:09

Michiel Buddingh


structure it generally the same way. Separate things into several files, each containing code which do related work.

Often with C, you can still think about objects. But instead of classes with methods, they are structures and functions which operate on a struct.

like image 21
Evan Teran Avatar answered Sep 28 '22 18:09

Evan Teran