I’m certain that more than 10 C programming mistakes are common, but for some reason people enjoy lists with 10 items. My list of Ten C Programming Mistakes isn’t a top-ten list, so nothing is ranked. These are just a collection (cut off at 10) of the problems and issues I find most frequently when coding in the C language.
Continue reading
Category Archives: Lesson
To Split a String in C
Have you ever heard a programmer mock the C language? Recently, a C# programmer informed me that C was okay, but “you can’t even split a string in C in less than 20 lines of code.”
Challenge accepted!
Continue reading
Stupid main() Function Tricks
The main() function is the first C language function you learn to write. It’s required. And you probably know that it has arguments, which are culled from the command prompt and made available to the program. So what stupid stuff can you do with main()?
Continue reading
Let’s Be Assertive
I find it fun to debug code. I add various printf() statements to a program to display values and quickly determine what’s going on. This process seems easier than toiling with a debugger.
Continue reading
Made-Up File Names
Many moons ago, I coded a file-renaming utility. It allows me to shuffle chapter names for documents used in my books, renaming a whole batch at once without overwriting existing filenames. It couldn’t happen if I didn’t know about the mktemp() function.
Continue reading
Factors
Factor is a scary word, but not really a mathematical monstrosity. If you have an integer value, its factors are the other integers that can divide evenly into the original value. The computer can deftly discover these values, providing you give it the proper C code to munch upon.
Continue reading
A Weird Thing I’ve Never Seen Before
While browsing some sample code recently, I encountered the following C language expression:
x = &(*ptr);
Both variables x and ptr are pointers. So what the heck is going on?
Continue reading
Looping with time_t values
In C, the time_t variable type is associated with the Unix epoch, or the number of seconds ticked since midnight, January 1, 1970. It’s typically a long int value, though as a typedef declaration, its specific data type could change in the future. Still, as a long int, you can play with it like other integers in your code.
Continue reading
Time After Time
While writing some code recently, it became necessary to translate timestamp strings into time_t or epoch time values. This value is the number of seconds that have ticked since midnight, January 1, 1970. time_t values are stored long integers (generally), but I needed a utility to translate them into time strings. It turned out, such a tool wasn’t that difficult to create.
Continue reading
Multiple Expressions in a for Condition
Every C programmer has screwed up a for loop’s statement. Having three arguments in one set of parentheses is just begging for trouble. That’s a lot of yard waste, but there’s a reason for all the detritus.
Continue reading