The challenge for this month’s Exercise is to split a decimal value into its integer and fractional portions. But what if you need the fractional portion as a string?
Continue reading
Splitting a Decimal Value – Solution
Cleaving a decimal value into its integer and fractional portions is a neat trick, one that relies upon C natural capability to thwack off the decimal portion of a float when it’s recast as an integer. Knowing about this conversion makes solving this month’s Exercise a whole lot easier.
Continue reading
Conditional Expressions Used as Values
It was weird when I first saw it: A conditional expression used to determine a value. It sounds odd, but it works.
Continue reading
Splitting a Decimal Value
Difficulty: ★ ★ ☆ ☆
A floating point, or real number, has two parts: Before the decimal comes the integer part. After the decimal is the fractional part. Sometimes it’s necessary to split such a value, so that your code can deal separately with the integer and fractional parts.
Continue reading
Outputting an Unterminated Buffer
Properly formed strings in C are terminated with the null character, \0
. Accept it or die!
However . . .
Continue reading
Desperately Freeing Allocated Memory
The critical issue about allocating and then freeing memory is to avoid a memory leak. This condition happens when a memory chunk gets lost, leaving the it lingering in RAM not doing anyone any good. Most often a memory leak occurs in a function.
Continue reading
Freeing Allocated Memory
One criticism I receive is that my code examples, from both my online training material as well as in my C programming books, fail to free any allocated memory before the program quits. This assertion is correct, and I have a darn good reason why!
Continue reading
C Blog 9th Anniversary
The first post on this blog was made on April 13, 2013.
Color Text, Part II
From last week’s Lesson, I showed how ANSI codes are used to set color text in terminal output. It’s time to go nuts showing the possibilities.
Continue reading
The Babylonian Square Root – Solution
I was blown away by the elegance and simplicity of the Babylonian Method to calculate a square root, which is the topic of this month’s Exercise. I only hope that my solution lives up the challenge, lest I suffer the fate of the Assyrians.
Continue reading