Program C Learning Resources

Based on the background of OO (C # / java) I am looking for resources to learn how to develop clean programs well.

As long as I am familiar with the C syntax, and I can write small programs, I’m not sure what to use for larger applications and what methods to use. Anything you guys can recommend.

EDIT: I am happy to completely abandon OO for programming in C, I am interested in how to structure a program without OO, I want to learn about good ways to develop programs in procedural languages ​​such as C.

+47
c design
May 29 '09 at 12:34
source share
7 answers

There is a list of unix books in this publication that includes most of the classic C / Unix works. For C programming in Windows Petzold Windows programming is probably the best start.

To develop a C program, some of the unix programming books will tell you snippets, but I don’t know about the C program architecture book.

If you are used to java, some tips for programming in C:

  • Make use stack . Often, when you call a procedure, you want to have the variables allocated in the frame of the caller’s stack and pass them pointers to the procedure that you want to call. This will be significantly faster than dynamic allocation of memory using malloc() and much less error prone. Do this when necessary.

  • C does not do garbage collection , so dynamically allocating data items is more complicated, and you need to track them to make sure they are freed. Variables allocated on the stack (see 1) are more “idiomatic” where they are applicable. In addition, you do not need to free them - this is a bonus for local variables.

  • Regarding (2), consider an architecture in which your functions return a status or error code and pass data to the stack and exit using the stack according to (1).

  • Find out what setjmp() and longjmp() . They can be very useful for general error handling mechanisms instead of the functionality for handling structured exceptions.

  • C does not support exceptions. See (3).

  • Lint is your friend. Splint is even more friendly.

  • Find out what preprocessor does and what you shouldn't do with it, even if you can.

  • Learn all the possibilities of endian-ness , word alignment , pointer arithmetic and other low-level architectural arcana. Contrary to popular belief, this is not rocket science. If you feel enthusiastic, try to learn assembly language and get good knowledge from it. It will do a lot for your understanding of what is going on in your C program.

  • C does not have a concept of the scope of a module, so plan your use of inclusions, prototype declarations, and use extern and static to make private scopes and import identifiers.

  • GUI programming in C is tedious on all platforms.

  • An attempt (10) to learn the C API of at least one scripting language such as Tcl , Lua, or Python. In many cases, the best use of C is the main high-performance application engine, which is largely written in something else.

  • The equivalent of a constructor is an initializing function in which you pass a pointer to the element you want to configure. Often you can see this as a function call that looks like setup_foo(&my_foo) . It is better to highlight the selection from the initialization, since you can use this function to initialize the element that you allocated on the stack. A similar principle applies to destructors.

  • Most people find the Hungarian notation that it reads as written by the Hungarian. An exception to this are native Hungarian speakers, who usually find Hungarian notation about as distinctly as Cuneiform. . Unfortunately, Hungarian notation is common in Windows software, and the entire Win32 API uses it, expecting to affect the readability of software written on this platform.

  • C / Unix books, even really good ones, such as those written by the late Richard Stevens, are generally available pretty cheaply on the Amazon market. In random order, do not get a copy of K & R, Stevens APUE and UNP 1 & 2, Dragon, Rochkind, Pearls, Petzold and Richter Programming (if running on Windows) and any other classic C / Unix. Read, draw on them with a pencil and generally interact with books.

  • There are many, many good C / Unix resources on the Internet on the Internet.

  • Read and understand the Ten Commandments of C Programming and some of the meta-discussions about why and why behind these commandments. It will show its age to a certain extent, although most of it is still relevant, and obscure compilers are still quite common in embedded systems world .

  • Lex and Yacc are your friend if you want to write parsers.

  • As Navicore points out below (+1) , Hanson 's C Interfaces and Implementations is a flawless interface / implementation design for a modular architecture with tons of examples. I really heard about this book and heard about it, although I cannot claim to have read it. In addition to the C-idioms I described above, this concept is perhaps the core of good procedural design. In fact, other procedural languages, such as Modula-2, really make this concept explicit in their design. This may be closest to the book "Software Architecture" in print. A.

  • Read the C FAQ .

+45
May 29 '09 at 13:53
source share

My problems with OO back to C were discussed in David Hanson's "C-Interfaces and Implementations."

C Interfaces and Implementations

Seriously, his approach made a huge difference in avoiding inadvertently creating a large ball of yarn that many non-oo-systems end up like.
+6
May 30 '09 at 2:09
source share

Here are some interesting answers from another question regarding OO programming in C. I made a post about some C code that I worked with, with which mostly accelerated object orientation only stops briefly without including virtual methods.

If I were doing C coding, I would use this technique to define "objects."

I believe that keeping Design Patterns in mind is always helpful and can be implemented in most languages.

Here is a good PDF discussing object oriented programming in C.

+2
May 29 '09 at 12:39
source share

minix by tanenbaum

+2
May 29 '09 at 12:45
source share

Larger applications? Strength is when you have to deal with low-level things, such as device drivers, schedulers, and other flavored OSs.

You can make C that works like OO, but it will feel like exercise when you re-create the wheel.

+1
May 30 '09 at 18:29
source share

One of the minor things is to order your .c files backwards - i.e. put main () at the end of the file and always ensure that local functions (those that are not in your .h file that you just wrote for use in this file) live above where they are used for the first time. This means that you do not need to write prototypes for them, which is not so important if you need to change your API.

Then, as a bonus, the "gD" command in vim will always go to the function definition if it is in the same file :)

+1
Jun 02 '09 at 6:06
source share

While it is written as a somewhat linguistic-agnostic text, Code Complete provides many good recommendations on the structure of the code and organization, with building practices.

0
May 30, '09 at 6:06
source share



All Articles