Unless the code must run endlessly, such as a program that operates a gas pump, an endless loop isn’t something you want. From last week’s Lesson, I crafted an endless loop to accept single-word input from the scanf() function to build a string. But no string is output because the loop never ends! It’s time to address this situation.
Continue reading
Category Archives: Lesson
Using scanf() to Build a String – Part II
Trying to salve my frustration with the scanf() function, I decided in last week’s Lesson to try to use scanf() to build a string. Because the function terminates standard input at the first whitespace character (space, tab, newline), the strings input must be stored and the string built in memory.
Continue reading
Using scanf() to Build a String – Part I
The scanf() function is useful for teaching, but it’s a booger. I avoid it outside of demonstration purposes. But it does provide good fodder for training beginning programmers to think about stream I/O.
Continue reading
Tally the Digits, Again
Difficulty: ★ ★ ☆ ☆
Seven years ago (to this same date!) I wrote an Exercise to tally the digits in a number. The solution required that the sum be reduced to a single digit. For example, the sum of digits in 12345 is 15, which is then reduced to six.
Continue reading
Outputting a Key Value
The power of an associative array is that it uses keys, text or numbers, to reference values. Know the key and you can fetch the value . This “association” works like an array in C, where you know the index or offset for an element. Though with an associative array, the index is a value or string — the key.
Continue reading
Dumping a Phony Associative Array
The first phony associative array function I need to write is a simple dump: Output the array’s contents in the form of pairs. Performing this operation requires a bit of manipulation to the way the array is presented in the code.
Continue reading
Associative Arrays
Being the ancient and venerable programming language it is, C lacks the associative array data type. This type of array is found in other languages, and it can be awkwardly simulated in C.
Continue reading
Pairs of Arrays
I enjoy studying foreign languages. A tool like Google Translate comes in handy, but it’s not perfect. That’s because computers translate words and phrases, but not the living, spoken language. Regardless, I thought I’d give language translation a stab, which got me into the topic of exploring arrays.
Continue reading
The Improved nano_delay() Function (I Hope)
Last week’s Lesson explored using the clock_gettime() function to pause program execution for a given number of nanoseconds. The code, however, contains a flaw that may render an inaccurate result.
Continue reading
Your Own Time Delay Function
I’ve written about my own delay() function, which uses processor clock ticks to calculate a time delay in milliseconds. Thanks to the clock_gettime() function, I can write a new time-delay function that operates from nanoseconds — or close to it. I hope.
Continue reading