Zig is an up-and-coming systems-oriented language that could one day replace C. Here’s what you need to know about Zig.
Zig, an emerging systems-oriented programming language with the potential to replace C. Zig, initiated by Andrew Kelley in 2015, is gaining momentum, aiming to combine the strengths of languages like C, C++, Rust, and Go while providing enhanced developer experiences and comparable performance.
In the realm of systems-oriented development, where languages like C and Rust prevail, Zig strives to offer a safer, less buggy, and more maintainable alternative. The language’s ambitious goal is to become the successor to the long-standing C programming language, serving as a portable low-level language and a benchmark for comparison.
Before delving into Zig’s programming specifics, the article explores its role in the broader programming language ecosystem. While Zig is categorized as a “low-level systems language,” its versatility extends beyond systems programming, making it suitable for embedded devices, WebAssembly targeting, game development, and various other tasks.
This insight draws parallels between Zig and C, emphasizing Zig’s potential to be a safer replacement for C with improved memory safety features and a more straightforward syntax. Despite competition with C, the article acknowledges a coexistence phase before Zig might supplant C on a broader scale.
Zig’s design goals focus on simplicity and ease of use. With a small feature footprint and a commitment to a single obvious way of doing things, Zig distinguishes itself. The language’s minimalism appeals to programmers, offering a focused syntax that aids in quick learning and application.
Zig’s memory handling stands out as it eschews direct memory allocation in favor of explicit handling in the standard library. The article emphasizes how this approach leads to clearer denotation of memory engagement, reducing hidden allocations and benefiting resource-limited and real-time environments.
Conditional compilation in Zig eliminates the need for a preprocessor, a departure from C’s macro-based approach. Zig’s compiler determines code evaluation at compilation time, enhancing readability and paving the way for optimizations. Notably, Zig introduces the comptime keyword, enabling compile-time execution for enforcing types against generics.
Highlighting Zig’s interoperability with C and C++, the article underscores its capability to compile and interoperate with these languages. Zig aims not only to replace C in syntax but also absorb C into itself, offering improved cross-compilation support and interaction with C libraries.
Zig’s error-handling system diverges from traditional exceptions, opting for a unique approach that avoids hidden control flow. The language incorporates a union type for error handling, providing explicit mechanisms for handling errors without relying on throw functions.
Zig’s toolchain includes a build tool that replaces traditional build tools like make and cmake, offering cross-platform functionality. The language’s testing support is integrated directly, simplifying the development and testing process.
Built on LLVM initially, Zig now considers LLVM an optional component, enhancing portability and freestanding builds. Despite not reaching version 1.0, Zig’s active community, extensive documentation, and evolving ecosystem make it a noteworthy project with anticipated production readiness.
In conclusion, Tectonic observes Zig’s gradual progress toward becoming a compelling language choice for developers, with its 1.0 release expected in 2025 or later. The language’s distinctive features, design goals, and community support position it as an intriguing project to monitor in the evolving landscape of programming languages.
Zig is a very active project. It was started by Andrew Kelley in 2015 and now seems to be reaching critical mass. Zig’s ambition is rather momentous in software history: to become the heir to the C programming language, which has reigned for decades as the go-to portable low-level language and as a standard to which other languages are compared.
Before we dive into the specifics of programming with Zig, let’s consider where it fits within the larger programming language ecosystem.
Zig is perhaps easiest to understand in relation to C, as a general purpose, non-garbage collected, and portable language with pointers. Today, virtually all programming infrastructure rests on C in various ways, including being the foundation of other programming languages like Java, JavaScript, and Python. Imagine the ripple effect of evolving to a language that is like C, but safer, less buggy, and easier to maintain. If Zig were to be adopted broadly as an archetypal replacement for C, it could have enormous systemic benefits.
Zig’s design goals and syntax
Zig is a “close to the metal” language in that it allows developers to work directly with system memory, a requirement for writing code that can be maximally optimized to its task. Direct memory allocation is a characteristic shared by the C family, Rust, and other low-level systems languages. Zig offers similar capabilities but aims to improve on them in several ways.
Zig seeks to be a simpler systems-oriented language than its predecessors and make it easier to write safe, correct code. It also aims for a better developer experience by reducing the sharp edges found in writing C-like software. On the first review, Zig’s features might not come across as earth-shattering, but the overall effect is of a safer platform that developers are finding easier to master and use.
Zig differs from most other languages in its small feature footprint, which is the outcome of an explicit design goal: Only one obvious way to do things. Zig’s developers take this goal so much to heart that for a time, Zig had no for loop, which was deemed an unnecessary syntactic elaboration upon the already adequate while loop.
How Zig handles memory
A distinctive feature of Zig is that it does not deal with memory allocation directly in the language. There is no malloc keyword like in C and C++. Instead, access to the heap is handled explicitly in the standard library. When you need such a feature, you pass in an Allocator object. This has the effect of clearly denoting when memory is being engaged by libraries while abstracting how it should be addressed. Instead, your client code determines what kind of allocator is appropriate.
Making memory access an obvious library characteristic is meant to avoid hidden allocations, which is a boon to resource-limited and real-time environments. Memory is lifted out of the language syntax, where it can appear anywhere, and its handling is made more explicit.
Zig also includes safety features for avoiding buffer overflows, and it ships with a debug allocator that detects memory leaks.
Conditional compilation in Zig
Zig uses conditional compilation, which eliminates the need for a preprocessor as found in C. Therefore, Zig does not have macros like C and C++. From a design standpoint, Zig’s development team views the need for a preprocessor as indicative of a language limitation that has been crudely patched over.
Instead of macros, Zig’s compiler determines what code can be evaluated at compilation time. For example, an if statement will actually eliminate its dead branch at compile time if possible. Instead of using #define to create a compile-time constant, Zig will determine if the const value can be treated that way and just do it. This not only makes code easier to read, write, and think about, but also opens up the opportunity for optimization.
Interoperability with C and C++
Zig sports a high degree of interoperability with C and C++. As the Zig docs acknowledge, “currently it is pragmatically true that C is the most versatile and portable language. Any language that does not have the ability to interact with C code risks obscurity.”
Zig can compile C and C++. It also ships with libc libraries for many platforms. It is able to build these without linking to external libc libraries. For a detailed discussion of Zig’s relationship to libc, see this Reddit thread.
Error handling in Zig
Zig has a unique error-handling system. As part of its “avoid hidden control flow” design philosophy, Zig doesn’t use throw to raise exceptions. The throw function can branch execution in ways that are hard to follow. Instead, if necessary, statements and functions can return an error type, as part of a union type with whatever is returned on the happy path. Code can use the error object to respond accordingly or use the try keyword to pass up the error.
State of Zig
Zig has an active Discord community and a lively GitHub ecosystem. The in-house documentation is pretty thorough. Zig users have produced a good amount of third-party material, as well.
Zig is not yet in 1.0 release, but its creators say it is approaching production ready. On the topic of readiness, Loris said, “Zig is not yet at v1.0, so things like webdev are still in their infancy, but the only usage that I would consider not recommending Zig for is data wrangling, for which I think a dynamic language like Python or Julia would be more practical.”
For now, the Zig team appears to be taking its time with the 1.0 release, which may drop in 2025 or later—but none of that stops us from building all sorts of things with the language today. Zig’s activity, goals, and uptake by the developer community make it a compelling project to watch.
Tectonic can’t help but wonder if there is a correlation to Salesforce releasing a new character, bearing the Zig name.
Meet Zig the Zebra, the Newest Salesforce Character
Get to know the latest member of the Salesforce family. She’s a creative and courageous go-getter, and she’s your guide to the Salesblazer Community.
All about Zig the Zebra
Name: Zig the Zebra
Height: 6’0
Pronouns: She/Her
Birthday: May 4, 2023
First Appearance: World Tour New York 2023
Favorite Badge: Sales Cloud Basics
Favorite Quote:
“Value the relationship more than the quota.”
Jeff Gitomer, author and sales expert
Who is Zig? And why a zebra?
Think of your favorite sports team, if you have one. Or even your favorite fast food chain. Chances are, the mascot you picture on the sidelines is the ultimate hype person. They hoot and holler, uplift and inspire, and give the community something to rally around.
Zig is that for Salesblazers. She travels in a dazzle (the word for a herd of zebras), makes a plan before making a move, and values relationships more than leading the pack. She also models the qualities of the quintessential Salesblazer: a dynamic, creative, courageous go-getter with a give-to-get philosophy. As Zig’s creator, Trailhead Executive Director Domenique Sillett Buxton, said, “Zig has earned her stripes within the sales industry and it’s now her mission to help bring Salesblazers together.”