I have had a lot of experience with Zig, and a bit of Assembly for a while now. Just 1 1/2 months ago I started coding in C, because it is a must-have for my upcoming job. I have a Zig project that decompresses a PNG from scratch, and I thought it would be a good learning experience to do the same with C. Tbh, I thought it will be a simple slide, but as it turns out; it really is not.
While Zig got heavily inspired by C, it differs from its features and API. C has a lot of headers for different things (such as stdio.h / stdlib.h / math.h), while Zig just has the std library, packaged with all of the other necessities (std.Io / std.math / std.crypto etc.). It was also interesting from going to using defer, errdefer, orelse, or other keywords in Zig that help in reducing bugs and make the code cleaner to read, to a lot less keywords. Looking at C89, C has 32 keywords, and Zig around 49 +/- 2 (not too sure). What that showed me, was that even with a lot of similarities, and the fact that I coded in Zig for 2 years, it was still difficult to get used to and learn C.
For me, one of the annoying parts was the fact, that I could never really remember what function or what struct is where. The fact that the FILE struct is located at stdio.h, while the DIR struct is in dirent.h, was something that bugged me for a while, but now after more than a month with C, is something I started to get along with.
Also, Makefiles are pretty darn cool. I have always been seeing them in big projects, and always wondered how they function. After coding a few projects and including a Makefile, I can safely say it is very nice seeing how your project is being made, after you type "make" and hit enter. The first time configuring the Makefile almost made me loose half my hair due to stress, but if you get it running properly once, then it is safe to say that it is gonna always run properly (especially for me, as my projects are somewhat small still).
What I came to appreciate more was the community in C. Thought I was gonna get insulted by some 59 year old grandpa, because I didn't make an out-of-bound check for my Pixels array, or because I didn't check whether or not the variable I de-noted with size_t exceeds SIZE_MAX - 1. Kinda the opposite. I learned a lot by sharing my projects on Reddit, a lot of smart people were quick to point out mistakes, and tell me where I could improve or where I went wrong etc.
A lot of stuff, that I have learned, was because of people telling me. Adding -Wall -Werror warnings, -fsanitize=address,undefined, -fno-omit-frame-pointer, among other things are the sole reason I debugged and fixed my programs a lot quicker. Especially -fsanitize was important to me, as I always try to keep memory usage low for my programs, and activating that warning, showed me where my memory leaked, or where overflows happened (yeah... out-of-bounds checks are actually pretty useful...).
With that being said. I still have a lot to learn in C. I realized that my code is not really C-esque (as some might say), which is mostly because I come from (prior to Zig) high-level languages such as Python, JavaScript or even GoLang. Me handling arrays with indexes instead of using the given possibility of pointers, shows me, that I still have a long way to go.
But I am certain with one thing, that being that, C is way more different than I thought.
Top comments (0)