diff --git a/example/hello_world/hello.zig b/example/hello_world/hello.zig index 126faf62fb9540f43ca2c7f96c15f85f23c1689c..cb7d5ef157e051c5f286a1014562caeab65c5bb4 100644 --- a/example/hello_world/hello.zig +++ b/example/hello_world/hello.zig @@ -1,5 +1,9 @@ const std = @import("std"); -pub fn main() void { - std.debug.warn("Hello, world!\n"); +pub fn main() !void { + // If this program is run without stdout attached, exit with an error. + const stdout_file = try std.io.getStdOut(); + // If this program encounters pipe failure when printing to stdout, exit + // with an error. + try stdout_file.write("Hello, world!\n"); } diff --git a/example/hello_world/hello_libc.zig b/example/hello_world/hello_libc.zig index d18b1c26b35b6fe9f00527dc04084778abd6c40c..05356bf5294f5f61986738fd770ec770c2c59100 100644 --- a/example/hello_world/hello_libc.zig +++ b/example/hello_world/hello_libc.zig @@ -2,9 +2,12 @@ const c = @cImport({ // See https://github.com/ziglang/zig/issues/515 @cDefine("_NO_CRT_STDIO_INLINE", "1"); @cInclude("stdio.h"); + @cInclude("string.h"); }); -export fn main(argc: c_int, argv: [*]?[*]u8) c_int { - _ = c.fprintf(c.stderr, c"Hello, world!\n"); +const msg = c"Hello, world!\n"; + +export fn main(argc: c_int, argv: **u8) c_int { + if (c.printf(msg) != @intCast(c_int, c.strlen(msg))) return -1; return 0; }