Created
October 22, 2025 22:58
-
-
Save nick133/392038169d8f9fc9011730bda3ffe483 to your computer and use it in GitHub Desktop.
printf() for Zig
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // Reliable C printf() replacement | |
| // Zig version 0.16.0-dev.747+493ad58ff | |
| // | |
| const std = @import("std"); | |
| fn printf(comptime fmt: []const u8, args: anytype) !void { | |
| var buf: [128]u8 = undefined; | |
| // Create a buffered writer for stdout using the buffer | |
| var fw = std.fs.File.stdout().writer(&buf); | |
| const writer: *std.Io.Writer = &fw.interface; | |
| try writer.print(fmt, args); | |
| try writer.flush(); | |
| } | |
| pub fn main() !void { | |
| try printf("Hello, {s}! The answer is {d}\n", .{"Zig", 42}); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment