This article has multiple issues. Please helpimprove it or discuss these issues on thetalk page.(Learn how and when to remove these messages) (Learn how and when to remove this message)
|
| Zig | |
|---|---|
| Paradigms | Multi-paradigm:imperative,concurrent,procedural,functional |
| Designed by | Andrew Kelley |
| First appeared | 8 February 2016; 9 years ago (2016-02-08)[1] |
| Stable release | |
| Typing discipline | Static,strong,inferred,nominal,generic |
| Memory management | Manual |
| Implementation language | Zig |
| Platform | x86-64,ARM64,WebAssembly Tier 2:ARM,IA-32,RISC-V,MIPS64,POWERPC64,SPARC64, some tier-2 platforms have tier-1 support forstandalone programs |
| OS | Cross-platform:Linux,macOS,FreeBSD,Windows |
| License | MIT |
| Filename extensions | .zig, .zir, .zigr, .zon |
| Website | ziglang |
| Influenced by | |
| C,C++,LLVM IR,Go,Rust[3][10] | |
Zig is animperative,general-purpose,statically typed,compiledsystemprogramming language designed by Andrew Kelley.[11] It isfree and open-source software, released under anMIT License.[12]
A major goal of the language is to improve on theC language,[13][14] with the intent of being even smaller and simpler to program in, while offering more functionality.[15] The improvements in language simplicity relate to flow control,function calls,library imports,variable declaration andUnicode support. Further, the language makes no use ofmacros orpreprocessor instructions. Features adopted from modern languages include the addition ofcompile timegeneric programmingdata types, allowing functions to work on a variety of data, along with a small set of newcompiler directives to allow access to the information about those types usingreflective programming (reflection).[16] Like C, Zig omitsgarbage collection, and hasmanual memory management.[16] To help eliminate the potential errors that arise in such systems, it includesoption types, a simplesyntax for using them, and aunit testing framework built into the language. Zig has many features forlow-level programming, notably packed structs (structs without padding between fields), arbitrary-width integers[17] and multiple pointer types.[18]
The main drawback of the system is that, although Zig has a growing community, as of 2025, it remains a new language with areas for improvement in maturity, ecosystem and tooling.[19] Also the learning curve for Zig can be steep, especially for those unfamiliar with low-level programming concepts.[19] The availability of learning resources is limited for complex use cases, though this is gradually improving as interest and adoption increase.[19] Other challenges mentioned by the reviewers are interoperability with other languages (extra effort to manage data marshaling and communication is required), as well as manual memory deallocation (disregarding proper memory management results directly in memory leaks).[19]
The development is funded by the Zig Software Foundation (ZSF), a non-profit corporation with Andrew Kelley as president, which accepts donations and hires multiple full-time employees.[20][21][22] Zig has very active contributor community, and is still in its early stages of development.[23] Despite this, aStack Overflow survey in 2024 found that Zig software developers earn salaries of $103,000 USD per year on average, making it one of the best-paying programming languages.[24] However, only 0.83% reported they were proficient in Zig.[23]
The primary goal of Zig is to be a better solution to the sorts of tasks that are currently solved with C. A primary concern in that respect is readability; Zig attempts to use existing concepts and syntax wherever possible, avoiding the addition of different syntax for similar concepts. Further, it is designed for "robustness, optimality and maintainability", including a variety of features to improve safety, optimization, and testing. The small and simple syntax is an important part of the maintenance, as it is a goal of the language to allow maintainers to debug the code without having to learn the intricacies of a language they might not be familiar with.[25] Even with these changes, Zig can compile into and against existing C code; C headers can be included in a Zig project and their functions called, and Zig code can be linked into C projects by including the compiler-built headers.[26]
In Zig, if something calls a function, it looks like a function call; if it doesn’t, it doesn’t look like a function call. If it can raise an error, it is explicit in the syntax.[26] Error handling is handled through error types and can be handled withcatch ortry. Zig has a more conservative extension of the type system, supporting compile time generics and accommodating a form ofduck typing with thecomptime directive.
One of the primary sources ofbugs in C programs is thememory management system, based onmalloc. malloc sets aside a block of memory for use in the code and returns a reference to that memory as apointer. There is no system to ensure that memory is released when the program no longer needs it, which can lead to programs using up all available memory, amemory leak. More common is adangling pointer that does not refer to a properly allocated memory object.[27]
A common solution to these problems is agarbage collector (GC), which examines the program for pointers to previously allocated memory, and removing any blocks that no longer have anything pointing to them. Although this greatly reduces, or even eliminates, memory errors, types of GC systems may have unpredictable performance that makes them unsuited toreal-time computing andsystems programming. Another solution isautomatic reference counting (ARC), which implements the same basic concept of identifying blocks of disused memory, but does so at pointer creation and destruction time by maintaining the number of pointers to a block, meaning there is no need to perform exhaustive pointer searches, which are rendered unnecessary at the cost of adding reference counter adjustment overhead to every pointer creation and destruction operation.[27]
Zig aims to provide performance similar to or better than C, so GC and ARC are not suitable solutions. Instead, it uses a concept known asoption types. Instead of a pointer being allowed to point to nothing, or nil, a separate type is used to indicate data that is optionally empty. This is similar to using a structure with a pointer and a boolean that indicates whether the pointer is valid, but the state of the boolean is invisibly managed by the language and does not need to be explicitly managed by the programmer. So, for instance, when the pointer is declared it is set to "unallocated", and when that pointer receives a value from a malloc, it is set to "allocated" if the malloc succeeded.[28]
The advantage to this model is that it has very low or zero overhead; the compiler has to create the code to pass along the optional type when pointers are manipulated, as opposed to a simple pointer, but this allows it to directly express possible memory problems at compile time with no runtime support. For instance, creating a pointer with a null value and then attempting to use it is perfectly acceptable in C, leading to null-pointer errors. In contrast, a language using optional types can check that all code paths only attempt to use pointers when they are valid. While this does not eliminate all potential problems, when issues do occur at runtime the error can be more precisely located and explained.[29]
Another change for memory management in Zig is that the actual allocation is handled throughstructs describing the action, as opposed to calling the memory management functions inlibc. For instance, in C if one wants to write a function that makes a string containing multiple copies of another string, the function might look like this:
constchar*repeat(constchar*original,size_ttimes);
In the code, the function would examine the size oforiginal and then malloctimes that length to set aside memory for the string it will build. That malloc is invisible to the functions calling it, if they fail to later release the memory, a leak will occur. In Zig, this might be handled using a function like:
constAllocator=std.mem.Allocator;fnrepeat(allocator:Allocator,original:[]constu8,times:usize)Allocator.Error![]constu8;
In this code, theallocator variable is passed a struct that describes what code should perform the allocation, and therepeat function returns either the resulting string or, using the optional type as indicated by the!, an Allocator.Error. By directly expressing the allocator as an input, memory allocation is never "hidden" within another function, it is always exposed to the API by the function that is ultimately calling for the memory to be allocated. No allocations are performed inside Zig’sstandard library. Further, as the struct can point to anything, one can use alternative allocators, even ones written in the program. This can allow, for instance, small-object allocators that do not use theoperating system functions that normally allocate an entirememory page.[30]
Optional types are an example of a language feature that offers general functionality while still being simple and generic. They do not have to be used to solve null pointer problems; they are also useful for any type of value where "no value" is an appropriate answer. Consider a functioncountTheNumberOfUsers that returns an integer, and an integer variable,theCountedUsers that holds the result. In many languages, amagic number would be placed intheCountedUsers to indicate thatcountTheNumberOfUsers has not yet been called, while many implementations would just set it to zero. In Zig, this could be implemented as anvartheCountedUsers:?i32=null which sets the variable to a clear "not been called" value.[30]
Another more general feature of Zig that also helps manage memory problems is the concept ofdefer, which marks some code to be performed at the end of a scope no matter what happens, including possible runtime errors. If a particular function allocates some memory and then disposes of it when the operation is complete, one can add a line to defer afree to ensure it is released no matter what happens.[30]
Zig memory management avoids hidden allocations. Allocation is not managed in the language directly. Instead, heap access is done via thestandard library, explicitly.[31]
Zig promotes a gradual approach to portability that combines new Zig code with existing C code. To do this, it aims to make interaction with existing C libraries as seamless as possible. Zig imports its own libraries with the@import directive, typically in this fashion:
conststd=@import("std");
Zig code within that file can now call functions inside std, for instance:
std.debug.print("Hello, world!\n",.{});
To work with C code, one simply replaces the@import with@cImport:
constc=@cImport(@cInclude("soundio/soundio.h"));
The Zig code can now call functions in the soundio library as if they were native Zig code. As Zig uses new data types that are explicitly defined, unlike C’s more genericint andfloat, a small number of directives are used to move data between the C and Zig types, including@intCast and@ptrCast.[30]
By using thecomptime keyword, the programmer can explicitly have Zig evaluate sections of code atcompile time, as opposed toruntime. Being able to run code at compile time allows Zig to have the functionality ofmacros andconditional compilation without the need for a separatepreprocessor language.[13]
During compile time, types becomefirst-class citizens. This enables compile-timeduck typing, and is how Zig implements generic types.[18]
For instance, in Zig, a genericlinked list type might be implemented using a function like:
fnLinkedList(comptimeT:type)type;
This function takes in some typeT, and returns a customstruct defining a linked list with that data type.
Zig also includes a C and C++ compiler, and can be used with either or both languages by leveraging with the commandszig cc andzig c++,[32] providing many headers including theC standard library (libc) andC++ Standard Library (libcxx) for many different platforms. This allows Zig’scc andc++ sub-commands to act ascross compilers out of the box (similarly toClang).[33][34]
Zig treats cross-compiling as a first-class use-case of the language.[16] This means any Zig compiler can compile runnable binaries for any of its target platforms, of which there are dozens. These include not only widely-used modern systems likeARM andx86-64, but alsoPowerPC,SPARC,MIPS,RISC-V,LoongArch64 and even the IBMz/Architectures (S390). The toolchain can compile to any of these targets without installing additional software, all the needed support is in the basic system.[30] The experimental support is also provided for less known platforms like AMD and Nvidia GPUs or PlayStation 4 and 5 (with various degree of support).
Cross-compilation is also available for variety of the operating systems (mostly desktop ones). Popular UNIX-like ones and Windows are officially supported (and documented), but (minimal) applications can and have been made for Android (withAndroid NDK) oriOS.[citation needed]
Zig usesLLVM (written in C++) as a backend for optimization. Since version 0.10 the Zig compiler is written in the Zig programming language, i.e., it is aself-hosting compiler. The self-hosted linker is tightly coupled with the self-hosted compiler.[citation needed]
TheLLVM backend is the default for most targets, except forSPIR-V, andx86-64[35] (although this is currently just in Debug mode). Zig also supports their self-hosted backend which can be enabled by using-fno-llvm.
Version 0.11.0 bundles an experimentalpackage manager, but no officialpackage repository is available. Instead a package is simply a URL that points to acompressed file, or aGitrepository. Each package ideally includes a standardbuild.zig file (that the Zig compiler uses by convention to compile the source code) and abuild.zig.zon file containing metadata with name and version of the package.
conststd=@import("std");constFile=std.fs.File;pubfnmain()!void{_=tryFile.stdout().write("Hello, World!\n");}
conststd=@import("std");constFormatOptions=std.fmt.FormatOptions;conststdout=std.io.getStdOut().writer();fnLinkedList(comptimeT:type)type{returnstruct{constSelf=@This();pubconstNode=struct{next:?*Node=null,data:T,};first:?*Node=null,pubfnprepend(list:*Self,new_node:*Node,)void{new_node.next=list.first;list.first=new_node;}pubfnformat(list:Self,comptimefmt:[]constu8,options:FormatOptions,out_stream:anytype,)!void{tryout_stream.writeAll("( ");varit=list.first;while(it)|node|:(it=node.next){trystd.fmt.formatType(node.data,fmt,options,out_stream,1,);tryout_stream.writeAll(" ");}tryout_stream.writeAll(")");}};}pubfnmain()!void{constListU32=LinkedList(u32);varlist=ListU32{};varnode1=ListU32.Node{.data=1};varnode2=ListU32.Node{.data=2};varnode3=ListU32.Node{.data=3};list.prepend(&node1);list.prepend(&node2);list.prepend(&node3);trystdout.print("{}\n",.{list});trystdout.print("{b}\n",.{list});}
( 3 2 1 )( 11 10 1 )
conststd=@import("std");constArenaAllocator=std.heap.ArenaAllocator;constAllocator=std.mem.Allocator;fnrepeat(allocator:Allocator,original:[]constu8,times:usize,)Allocator.Error![]u8{constbuffer=tryallocator.alloc(u8,original.len*times,);for(0..times)|i|{std.mem.copyForwards(u8,buffer[(original.len*i)..],original,);}returnbuffer;}pubfnmain()!void{vararena=ArenaAllocator.init(std.heap.page_allocator,);deferarena.deinit();constallocator=arena.allocator();constoriginal="Hello ";constrepeated=tryrepeat(allocator,original,3,);std.debug.print("{s}\n",.{repeated});}
Hello Hello HelloThe name “Zig” was reportedly chosen through a process involving aPython script that randomly combined letters, starting with the letter “Z” and followed by a vowel or “Y”, in order to generate four-letter words. Despite the intended length, “Zig”, a three-letter word, was ultimately selected from the various combinations produced by the script.[36]
The previousbootstrapping compiler, written in Zig and C++ usingLLVM as a back-end,[37][38] supporting many of its native targets,[39] was removed in version 0.11. Newer versions of Zig use a prebuiltWebAssembly version of Zig to bootstrap itself.
On 26 November 2025, Andrew Kelley announced that the Zig organization would be migrating fromGitHub toCodeberg.[41]