I refer to structures as “multi-variables” in my books and courses. Like a mini-database, they hold different data types and values, all bundled into a single unity. Structures form the basis of important programming concepts such as a linked list. Further, you can use structures to cheat and return multiple values from a function. As much as I dislike admitting it, structures are fun.
Continue reading
Author Archives: dgookin
More Than One String in a String
I’ve seen some oddball constructions in C. You may have as well, especially if you enjoy reading obfuscated C. Yet, the weirdness I just witnessed came from an online C course I was browsing. I’d never seen it before.
Continue reading
Trigraph Sequences
I doubt you’ve ever used a trigraph. If you saw a trigraph in some C code, you might assume it was a typo or, from the early days of telecommunications, a modem burp. But trigraphs present a legitimate if not arcane way to represent certain characters, a holdover from the days of teletype input and primitive, barely-ASCII keyboards.
Continue reading
Misused Placeholders
I received a question from a reader about improperly specifying a printf() placeholder. Specifically, he used %d (decimal integer) to output a string. Most compilers flag this condition as a warning, mismatched types or something similar. Still, the program is created and it runs. What does the output mean?
Continue reading
Alphabedecimal Revisited – Solution
The challenge for this month’s Exercise is to count in “alphabedecimal,” generating output from AAAA through ZZZZ, without using nested loops or a complex if structure. Your solution must use a recursive function that handles flipping the digits, er, characters.
Continue reading
Frying a String
The challenge for September’s Exercise is to scramble a string, jumbling its characters in a random pattern. I figured it’s a fun exercise, not anything useless beyond curiosity. Yet such a function exists for the GNU C library, strfry().
Continue reading
Alphabedecimal Revisited
Difficulty: Medium
The goal of the November 2018 Exercise on this blog was to count in “alphabedecimal,” or base 26 using the uppercase letters of the alphabet. The solution cycles from AAAA to ZZZZ flipping each character (or digit) one at a time, just as numbers are counted. The challenge is to do so without employing a nested loop.
Continue reading
The strerror() Function
System errors happen. Your program accesses the operating system and . . . something goes wrong. When it does, the function returns -1 and your code must rely upon our old buddy errno to discover what went wrong and possibly output an informative error message.
Continue reading
Magic Numbers
There’s a scene in the HBO series Silicon Valley that aptly describes a magic number in programming code: Coders are trying to figure out a compression algorithm and they encounter a large integer value. They wonder what it means; it’s not commented or assigned to a clever constant name. It’s just . . . a magic number.
Continue reading
Is it Bad to Cast malloc()?
In my code, in my books, and in my online courses, I typecast the malloc() function. This is something I’ve done for a while, but never really knew why — until now. I’ve also learned that doing so is considered “bad programming practice” by some in the C community.
Continue reading