Your C programs’ text output need not be so dull. Aside from adding wide characters, you can spice things up with color text. The terminal flavor is what determines the color palette. All you need to know are the secret codes that activate and deactivate the attributes.
Continue reading
Author Archives: dgookin
The Babylonian Square Root

Difficulty: ★ ★ ★ ☆
When I sought to write my own square root function back in 2020, I based it on a series of steps from the math.com website. I figured, “Hey! These guys are math nerds. They know their stuff.” Turns out, the ancient Babylonians beat them to the punch — and have a better method for calculating a square root.
Continue reading
Accessing the Printer in C
Back in the old days, the printer was a device wired to the computer and handled directly by whatever program wanted to use it. Printing in C involved opening the printer device and sending the data. Today, things work differently.
Continue reading
Initializing a Buffer – Your Own memset() Function
As I wrote in last week’s Lesson, the C23 standard renders the memset() function — a standard C library function for over 30 years — obsolete. If you want to initialize a buffer, you can use the secure memset_s() or just initialize the buffer manually or with your own memset()-like function.
Continue reading
The Perils of the memset() Function
While researching the upcoming — and significant — C23 version of the C programming language, I learned something surprising: The memset() function will be deprecated. It effectively does nothing when used in the C23 standard. The reason makes a lot of sense.
Continue reading
Simplifying Fractions – Solution
This month’s Exercise both terrified and enticed me. Yes, I code these solutions myself after I think up the challenges. No cheating here! This particular puzzle was more fun than I imagined, but also required a lot of mental work.
Continue reading
How Many Characters Output – So Far?
I doubt any C programmer has memorized all of the printf() function’s placeholders, options, and settings. It’s a character salad of interesting and weird things. One of the strangest has to be the %n placeholder.
Continue reading
Simplifying Fractions
Difficulty: ★ ★ ★ ☆
Recently, I wrote code to calculate odds. The program’s output listed the odds as a fraction, such as 1/6 for rolling a specific number on a die. But occasionally, the output is something like 7/21. As a human, and after a modicum of mental sweat, I recognize 7/21 as 1/3. The challenge is how to get the computer to recognize and report the smaller fraction.
Continue reading
How Many Characters Output?
I don’t think I’ve ever used the return value from a printf() statement. Yes, the function has an int return value, which is the number of characters output. This value can be difficult to guess.
Continue reading
Variable Scope
In C programming, variables declared within a function are local to the function. Specifically, they’re the auto storage class, the default. External, or global, variables are defined outside of a function declaration and are of the extern storage class. These variables are available to all functions. But the scope within a function can also be limited, depending on where the variable is declared.
Continue reading