What are some concepts/techniques/language features that every decent C programmer should know/be aware of (exclude general software engineering and similar and focus only on C specific stuff). I would like to know so that I would be able to fill in some possible gaps in my C knowledge.
closed as not constructive by gnat, Giorgio, thorsten müller, GlenH7, Jalayn Apr 20 at 13:45
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or specific expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, see the FAQ for guidance.
|
Specific to C? Aside from standard constructs common to most procedural languages, I'd have to say:
|
|||||||||||
|
|
Understand pointers and you will understand computers. |
|||||
|
|
In addition to pythagras's excellent answer, how to write (or at least read) complicated declarations, such as funcs is an array[4] of pointers to a function returning pointer to array[10] of char |
|||
|
A C programmer should know... other languages! ;-) It is always fruitful to know concepts from others languages of various paradigms, like OOP, functional programming, and so one. More seriously, a look at the obfuscated programming contest is fun and, curiously, a good experience too. |
|||
|
|
|
|||
|
|
|
I mentioned "buffer overflows" in a comment to Pythagras's answer, I should probably clarify what I meant a bit. In C, it's not enough to know that working directly with memory is dangerous - you should also understand the precise ways in which it's dangerous. I don't really like the "shooting yourself in the foot" metaphor for all of these cases - a lot of the time, it's not you pulling the trigger, but often it's an actor with interests contrary to yours and/or your users'. For example, in an architecture with a descending stack (most popular architectures fit this bill - x86 and ARM generally included), when you call a function, the return address for the function will be placed on the stack after the local variables defined in the body of the function. So if you declare a buffer as a local variable, and expose that variable to the outside world without checking for buffer overflow, like this:
an external user can send you a string that overwrites the return address from the stack - basically, he can change your program's run-time idea of the call-graph that lead to the current function. So the user gives you a string that's the binary representation of some executable code for your architecture, enough padding to overflow the stack from C exposes you to low-level details about how your machine works, and it gives you more direct control over your machine than any other user-edited language in widespread use today. With great power comes great responsibility - you actually need to understand those low-level details in order to work with C safely and effectively. |
|||
|
|
|
In addition to the other good answers I would like to add defensive programming techniques to the list. E.g. using asserts at start/end of functions to verify contract. |
|||
|
|

2 + 2 = 4– Crazy Eddie Feb 15 '11 at 20:48