Today we will be discussing how Zig is a C competitor and why I think it will (possibly) in the future surpass C in terms of popularity among other factors, This post will go in depth into many features of the language, but more importantly specific bullet points that make it stand out from being "just another C".

Cross Compilation

One issue I've personally always had with C is that cross compiling in it is sometimes a real pain in the ass, one major thing that contributes to this factor is that there is so many make/project generators, examples of these being CMake, Premake, etc. Another large contributor is due to the fact that not only is there multiple C runtimes, there is multiple C compilers and libraries (glibc, musl, etc). The problems with this are:

  1. libc is usually if not always dynamically linked, what this means is that you have to have a shared object (.s0), dynamic linked library (.dll), etc linked in your PATH or other environment variable, for the compiled executable to even launch.
  2. when working on projects, if you choose to go with a lesser used project generator for whatever reason, such as Premake vs CMake, due to most projects using the latter, you'd have to write generator files for those projects as well, and this can become extremely time consuming.
  3. Choosing the runtime and compiler can be a hassle due to one not always being compatible with the other.

and finally the fact there is multiple standard libraries for C, they may not be cross compatible, meaning in the end you may get a broken executable if you switch for whatever reason.

Zig defeats all these bullet points by not having it's own building system and script, but has it's own standard library built into the language as many other languages do, as well as while it can compile using libc, it does not depend on it. On top of this Zig can compile to multiple platforms from a single one without needing to be on the operating systems/platforms you're compiling for.

Error handling

Error handling in C is a mess, in the conference labeled "The Road To Zig 1.0", Andrew Kelley discusses multiple negatives one major one being how C developers and the language itself handles errors. One scary (but hilarious) case was where when using some system calls in C to open and write to a file that doesn't exist, C failed to open the file, and instead of breaking the program, it ran fine as if everything worked according to plan, but using strace to check what actually happened, it was revealed that it was trying to write to file descriptor -1 which isn't valid, and yet still ran "successfully".

In Zig error handling is mandatory if something can throw an error, it must properly be handled, otherwise Zig while not compile in the first place, thus preventing unintended behavior from happening at runtime, and preventing weird bugs and errors that shouldn't exactly happen when an end user runs your software. You can read more about Zig errors here.

Safety And Speed

I've noticed personally in C, that a lot of the time you have to sacrifice either speed or safety in your programs when compiling and only use one a lot of the time as it strips a lot of things for debugging among other things. Zig has 4 different build configurations, as can be seen below:

Configurations

In the Debug configuration, you sacrifice speed and size, while getting the most safety and the program will crash instead of allowing undefined behavior to happen. In the ReleaseSafe configuration the behavior of Debug is still prevalent but optimizations are also turned on, becoming a middle ground between speed and safety. In ReleaseFast the optimizations are full-on and debug symbols and safety are turned off, undefined behavior can happen, but the program will be heavily optimized. Last but not least in ReleaseSmall the executable is optimized to be compiled into a file as small as it can be possibly compiled to.

Conclusion

There is many other reasons I love the language, such as requiring values to be known at compile time, order independent code declaration, among other things you can read up on, on the website. Please give the language a shot, and while it is still very early in development, it is an amazing project and language overall, and maybe, just maybe, someday it will take over C.