Last year, I wrote about using the standard error device stderr to generate output that can’t be redirected. This technique is often used to send error messages to the console, though I noted how these messages may show up out of sync with standard output. This situation can be resolved, providing you know how to flush the stderr device buffer.
Continue reading
Category Archives: Lesson
Putting strtol() to the Test
Stop using the atoi() function! Yes, it’s a golden oldie, often my go-to function for converting strings to integers, but it’s weak. The strtol() function, on the other hand, not only converts strings to integers, it can validate input as well.
Continue reading
Peculiarities of the strtol() Function
Imagine my disappointment to learn that the atoi() function, one of the first I learned in C, is actually a macro. I’m horrified. As much as I treasure converting strings to integers, I shall survive this life-changing discovery.
Continue reading
Exponentiation
The C language lacks an exponentiation operator, which would raise a base value to a certain power. For example: 2^8, which in some programming languages is an expression to raise 2 to the 8th power. Instead, C uses the pow() function.
Continue reading
The Perils of strncpy()
Always be careful when manipulating strings! Tack on that null character, '\0', at the end of a string! And don’t worry about the C language string functions: They perform this important feature for you. But do they all?
Continue reading
To Read a Chunk a Data
Say you must read 2048 bytes from a file. Which of the following fread() statements works for you: A, B, both, or neither?
A. c = fread( buffer, 2048, 1, fh);
B. c = fread( buffer, 1, 2048, fh);
Continue reading
Old Habits Die Hard
Like doing anything for a long period of time, I find myself often using the same approach to solving a programming puzzle. It’s habit. It’s tradition. And it often hinders my insight into the language.
Continue reading
Slicing Words from a String
The fall-through effect of the switch-case structure can be extremely useful, as demonstrated in last week’s Lesson. For slicing words from a string, you can easily match multiple word separators, saving you from coding complex statements or weirdo if-else structures. But a problem arises when multiple word separators appear.
Continue reading
Falling Through a Switch-Case Structure
C language keywords often used in a switch-case structure are switch, case, default, and break. Only switch and case are required. You can omit the default condition. And the break keyword is used to avoid execution falling through the structure, yet sometimes that may be a useful effect.
Continue reading
Switch-Case Execution
The switch-case structure is the most complex decision tool in the C language. Yes, the ternary operator is more cryptic, but a proper switch-case structure involves more keywords: switch, case, default, and break. The break keyword is what I feel makes the structure most interesting.
Continue reading