I have used C for my own software development tasks. I have also taught a university-level C course (well, this isn’t really relevant, but some think otherwise. Oh, well). Here are my tips based on my observation of common mistakes (including those I’ve done myself). I hope the following tips can reduce the occurrences of those mistakes.
-
Be sure to
fclose()
after a successfulfopen()
. Similarly for*alloc()
andfree()
. -
An easy way to
malloc()
:ptr = malloc(NUM * sizeof *ptr);
This is good as we don’t need to care about the type of
ptr
. Note thatptr
won’t be dereferenced since it’s used as an operand tosizeof
, hence you don’t need to check whether its value isNULL
.