Commit 5de07ab721
Changed files (2)
example
hello_world
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");
}
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;
}