Weird output on a simple program

So I’m just going through and trying little projects to to learn Zig better. I’m trying to do as much as I can from memory to get better at it. I made a simple program that adds two numbers together. I’m getting some weird output. Sometimes I get the right answer sometimes a get letters like “st”.

If i change the numbers at all I and run zig build run i get letters, but if i type zig build run a second time the correct answer prints. This is just me learning by small excercises.

i think you need a formatting specifier on your print function
std.debug.print(“{d}”, .{answer});

{d} is for decimal output

There’s more information on the specifiers in the std.fmt docs, near the top

I tried that but I’m still getting the weird output. I thought maybe it might be the root.zig file giving me the issue since it has an add function in it so I got rid of that. I ran zig build run again but still had the st letters then the real answer. I then tried running the main file directly with zig run main.zig and it worked everytime, so I thought it was a cache issue. I deleted the cache and out files to rebuild the program but that didnt fix it.

so it works right when i run zig run main.zig but not when i do zig build run.

Alright. I can confirm I am getting this same behavior on WSL. Seems to be working fine on regular Windows console.

Im on windows and its being wonky with me. try changing what the number is and rerunning zig build run

okay, so if you change your print function to print newlines before and after like this…

std.debug.print(“\n{d}\n”, .{answer});

then you should something like the following output.

zig build run
steps [4/7] install Desktop…

the weird letters are from running the steps. but it’s being overwritten as well as the output. so it’s working fine just you have the result getting overwritten.

this is sometimes an issue in different places. i’ve just been working around it by printing newlines.


Ok thank you, I never would have thought of that…could this be considered a bug? It doesnt make sense to me that I would have to have newlines before and after. I haven’t seen that.

Although this output clobbering when running zig build run is far from ideal, it’s actually separate from the output of your program so you should see the correct output by doing just zig build and then running the actual binary placed in zig-out/bin. No need for the extra newlines in that case.


for testing purposes doesn’t this make it kind of hard? When I try to run the binary it just closes immediately.

It sounds like you’re running it by double-clicking on the binary? That would be consistent with it closing immediately. You would have to run it manually (./zig-out/bin/binary_name) in a terminal / command prompt / PowerShell to see the output.

As far as work flow goes when youre building something do you use zig build run or go to the binary

I use zig build run pretty much exclusively while debugging, but you have to be aware that since using std.debug.print is meant for debugging output and thus prints to standard error, the output will be clobbered by othere debug output like the one produced by zig build run itself. In that case and keeping within the debugging mindset, adding a couple extra newlines to make some space is acceptable. I also sometimes add a prefix "---> DEBUG: " to really help me find the output in busy screens. But rest assured that when you produce the final release, you should be using standard out and not have to do any of these tricks to get proper output.


Thank you! That makes sense. Also love the videos youre putting out. Keep them coming.


If i wanted to open this as an issue on github how would word this, ive never done that before?

I found a similar issue here, though this one is referring to output from the test block. You can probably follow the model for that.


Thats perfect thank you, i would describe as something like missing newline after std.debug.print, or probably after zig build run while using std.debug.print

i think technically what’s missing is what’s coming before the std.debug.print, or before the regular output. like somewhere between what the compiler is logging to the console and the first line of output from your application. or you could say your output is being overwritten/clobbered by the output coming from the compiler when running zig build run.


Ill make sure to share it in this thread before posting to make sure it sounds right


After going through the current and closed issues I found some similar ones with talks about it not being necessary to change, so I wont be adding another issue for it.

ok. that sounds about right. personally I think there is something there, which needs to be addressed. but it’s not going to be simple like just printing an extra newline. since Zig is still highly experimental, it might take some time before we know the best way to resolve the issue. it might require a better perspective which we don’t have right now.

in the meantime… based on my personal experience with the language, I also had a few issues where I was saying hey i need to do this for testing purposes, like printing some random info. but really what I was saying is I need to do this for my “personal testing” purposes. which is not necessarily how you run actual tests in systems programming. so I would have to adjust how I think about programming, and what makes well-written code. Zig is really progressive about this, since the compiler and the structure of the language will encourage you to adhere to certain patterns, which might not be immediately apparent if you are coming from say a c++ background.

looking back at your original code, you might want to take advantage of Zig’s builtin testing capabilities here. instead of printing the output, you could call std.testing.expectEqual() to do a comparison with the answer, then when you run it if something goes wrong the compiler will let you know.

so replace std.debug.print("{}",.{answer}); with try std.testing.expectEqual(answer, 15);. This will run a comparison check to verify that the answer is correct, and if it’s wrong it will also tell you what answer is expected it to be.