Writing Type-Safe Generics In C

The fun part about a programming language like C is that although the language doesn’t directly support many features including object-oriented programming and generics, there’s nothing that’s keeping you from implementing said features in C. This extends to something like type-safe generics in C, as [Raph] demonstrates in a blog post.

After running through the various ways that generics are also being implemented using methods including basic preprocessor macros and void pointers, the demonstrated method is introduced. While not necessarily a new one, the advantage with this method is that it is type-safe. Much like C++ templates, these generics are evaluated at compile time, with the preprocessor handling both the type checking and filling in of the right template snippets.

While somewhat verbose, it can be condensed into a single header file, doesn’t rely on the void type or pointers and can be deduplicated by the linker, preventing bloat. If generics is what you are looking for in your C project, this might be a conceivable solution.

Dark lab setup with scientific looking drink dispenser

Scared For A Drink?

Halloween is about tricks and treats, but who wouldn’t fancy a bit to drink with that? [John Sutley] decided to complete his Halloween party with a drink dispenser looking as though it was dumped by a backstreet laboratory. It’s not only an impressive looking separating funnel, it even runs on an Arduino. The setup combines lab glassware, servo motors, and an industrial control panel straight from a process plant.

The power management appeared the most challenging part. The three servos drew more current than one Arduino could handle. [John] overcame voltage sag, brownouts, and ghostly resets. A healthy 1000 µF capacitor across the 5-volt rail fixed it. With a bit of PWM control and some C++, [John] managed to finish up his interactive bar system where guests could seal their own doom by pressing simple buttons.

This combines the thrill of Halloween with ‘the ghost in the machine’. Going past the question whether you should ever drink from a test tube – what color would you pick? Lingonberry juice or aqua regia, who could tell? From this video, we wouldn’t trust the bartender on it – but build it yourself and see what it brings you!

Continue reading “Scared For A Drink?”

Hello World In C Without Linking In Libraries

If there’s one constant with software developers, it is that sometimes they get bored. At these times, they tend to think dangerous thoughts, usually starting with ‘What if…’. Next you know, they have gone down a dark and winding rabbit hole and found themselves staring at something so amazing that the only natural conclusion that comes to mind is that while educational, it serves no immediate purpose.

The idea of applying this to snipping out the <stdio.h> header in C and the printf() function that it provides definitely is a good example here. Starting from the typical Hello World example in C, [Old Man Yells at Code] over at YouTube first takes us from the standard dynamically linked binary at a bloated 16 kB, to the statically linked version at an eyepopping 767 kB.

To remove any such dynamic linkages, and to keep file sizes somewhat sane, he then proceeds to first use the write()function from the <unistd.h> header, which does indeed cut out the <stdio.h> include, before doing the reasonable thing and removing all includes by rewriting the code in x86 assembly.

While this gets the final binary size down to 9 kB and needs no libraries to link with, it still performs a syscall, after setting appropriate register values, to hand control back to the kernel for doing the actual printing. If you try doing something similar with syscall(), you have to link in libc, so it might very well be that this is the real way to do Hello World without includes or linking in libraries. Plus the asm keyword is part of C, although one could argue that at this point you could just as well write everything in x86 ASM.

Of course, one cannot argue that this experience isn’t incredibly educational, and decidedly answers the original ‘What if…’ question.

Continue reading “Hello World In C Without Linking In Libraries”

C Project Turns Into Full-Fledged OS

While some of us may have learned C in order to interact with embedded electronics or deep with computing hardware of some sort, others learn C for the challenge alone. Compared to newer languages like Python there’s a lot that C leaves up to the programmer that can be incredibly daunting. At the beginning of the year [Ethan] set out with a goal of learning C for its own sake and ended up with a working operating system from scratch programmed in not only C but Assembly as well.

[Ethan] calls his project Moderate Overdose of System Eccentricity, or MooseOS. Original programming and testing was done in QEMU on a Mac where he was able to build all of the core components of the operating system one-by-one including a kernel, a basic filesystem, and drivers for PS/2 peripherals as well as 320×200 VGA video. It also includes a dock-based GUI with design cues from operating systems like Macintosh System 1. From that GUI users can launch a few applications, from a text editor, a file explorer, or a terminal. There’s plenty of additional information about this OS on his GitHub page as well as a separate blog post.

The project didn’t stay confined to the QEMU virtual machine either. A friend of his was throwing away a 2009-era desktop which [Ethan] quickly grabbed to test his operating system on bare metal. There was just one fault that the real hardware threw that QEMU never did, but with a bit of troubleshooting it was able to run. He also notes that this was inspired by a wiki called OSDev which, although a bit dated now, is a great place to go to learn about the fundamentals of operating systems. We’d also recommend checking out this project that performs a similar task but on the RISC-V instruction set instead.

UNIX For A Legacy TI

Although now mostly known as a company who cornered the market on graphing calculators while only updating them once a decade or so, there was a time when Texas Instruments was a major force in the computing world. In the late 70s and early 80s they released a line of computers called the TI-99 to compete (unsuccessfully) with various offerings from Commodore, and these machines were fairly robust for the time. They did have limited memory but offered a 16-bit CPU and plenty of peripherals, and now there’s even a UNIX-like OS that they can run.

This version of UNIX is called UNIX99 and is the brainchild of AtariAge forum member [mrvan] who originally wasn’t looking to develop a full operating system for this computer but rather a set of standard C libraries to help with other projects. Apparently the step from that to a UNIX-flavored OS wasn’t too big so this project was born. While the operating system doesn’t have a UNIX certification, it has most of the tools any of us would recognize on similar machines. The OS has support for most of the TI-99 hardware, file management, a basic user account system, and a command shell through which scripts can be written and executed.

That being said, the limitations of the hardware do come through in the operating system. There’s no multitasking, for example, and the small amount of memory is a major hurdle as well. But that’s what makes this project all the more impressive, and [mrvan] isn’t stopping here. He’s working on a few other improvements to this platform, and we look forward to seeing future releases. UNIX itself is extremely influential in the computing world, and has been used a the model for other homebrew UNIX-like operating systems on similar platforms of this era such as the Z80.

Thanks to [Stephen] for the tip!

Photo courtesy of Rama & Musée Bolo via Wikimedia Commons

Learn C With A Lisp

One reason Forth remains popular is that it is very simple to create, but also very powerful. But there’s an even older language that can make the same claim: LISP. Sure, some people think that’s an acronym for “lots of irritating spurious parenthesis,” but if you can get past the strange syntax, the language is elegant and deceptively simple, at least at its core. Now, [Daniel Holden] challenges you to build your own Lisp as a way to learn C programming.

It shouldn’t be surprising that LISP is fairly simple. It was the second-oldest language, showing up in the late 1950s with implementations in the early 1960s. The old hardware couldn’t do much by today’s standards, so it is reasonable that LISP has to be somewhat economical.

With LISP, everything is a list, which means you can freely treat code as data and manipulate it. Lists can contain items like symbols, numbers, and other lists. This is somewhat annoying to C, which likes things to have particular types, so that’s one challenge to writing the code.

While we know a little LISP, we aren’t completely sold that building your own is a good way to learn C. But if you like LISP, it might be good motivation. We might be more inclined to suggest Jones on Forth as a good language project, but, then again, it is good to have choices. Of course, you could choose not to choose and try Forsp.

Coroutines In C

It is virtually a rite of passage for C programmers to realize that they can write their own cooperative multitasking system. C is low-level enough, and there are several ways to approach the problem, so, like Jedi light sabers, each one is a little bit different. [Christoph Wolcher] took his turn, and not only is his system an elegant hack, if that’s not an oxymoron, it is also extremely well documented.

Before you dig in, be warned. [Christoph] fully admits that you should use an RTOS. Or Rust. Besides, after he finished, he discovered the protothreads library, which does a similar task in a different way that is both more cool and more terrible all at the same time.

Once you dig in, though, you’ll see the system relies on state machines. Just to prove the point, he writes a basic implementation, which is fine, but hard to parse and modify. Then he shows a simple implementation using FreeRTOS, which is fine except for, you know, needing FreeRTOS.

Using a simple set of macros, it is possible to get something very similar to the RTOS version that runs independently, like the original version. Most of the long code snippets show you what code the macros generate. The real code is short and to the point.

Multiprocessing is a big topic. You can have processes, threads, fibers, and coroutines. Each has its pros and cons, and each has its place in your toolbox.