System errors happen. Your program accesses the operating system and . . . something goes wrong. When it does, the function returns -1 and your code must rely upon our old buddy errno to discover what went wrong and possibly output an informative error message.
Continue reading
Category Archives: Lesson
Magic Numbers
There’s a scene in the HBO series Silicon Valley that aptly describes a magic number in programming code: Coders are trying to figure out a compression algorithm and they encounter a large integer value. They wonder what it means; it’s not commented or assigned to a clever constant name. It’s just . . . a magic number.
Continue reading
Is it Bad to Cast malloc()?
In my code, in my books, and in my online courses, I typecast the malloc() function. This is something I’ve done for a while, but never really knew why — until now. I’ve also learned that doing so is considered “bad programming practice” by some in the C community.
Continue reading
When Passing a String to a Function, Be Careful That You Don’t . . .
Passing a string to a function? Check!
Passing an array of strings to a function? Watch out!
Continue reading
Manipulating a String in a Function
You have two choices when modifying a string in a function: You can modify the string directly or you can create a new string and return it. Either way, a new string is created based on the old.
Continue reading
Passing Strings to a Function
Despite teaching the C language, I still find myself at odds with pointers. Specifically, it’s the double pointers that remind me of my mortality. I found myself getting into the double pointer polka recently when I tried to work with an array of strings, passing each one individually to a function. O! The pain!
Continue reading
Negative Array Elements
I enjoy it when an experienced coder reviews my stuff. I’m happy with the feedback, and occasionally they toss me a bone. This time it was a rather obscure bone, but I see the point: Avoid using negative array elements.
Continue reading
Characters, Values, and Playing with Your Brain
I’ve messed with characters as values quite a few times in my code. Keeping in mind that the char data type is really a tiny integer value, you can perform all kinds of tricks — stuff that drives non-programmers crazy.
Continue reading
Reading strings with the sscanf() Function
I’m not a fan of the scanf() function. It’s an input function, quick enough to toss out there for a beginner to write a (somewhat) interactive programs. But the function itself is horrid, with complex arguments and dubious results.
So imagine my delight at finding its companion function, sscanf().
Continue reading
Discovering Command Line Options, Part V
Somewhere along the line, shell commands developed longer, verbose versions of their original, short command line switches. So in addition to -o you also could use --output. These switches offer readability and are easier to remember. Alas, the getopt() function doesn’t process them, but its sibling function getopt_long() does.
Continue reading