From efbb6128bbfdd0b731578dcc55d4a55732d20d8a Mon Sep 17 00:00:00 2001
From: Manlio Perillo
C code
- {#syntax_block|c|call_malloc_in_c.c#}// malloc prototype included for reference + {#syntax_block|c|call_malloc_in_c.c#} +// malloc prototype included for reference void *malloc(size_t size); struct Foo *do_a_thing(void) { char *ptr = malloc(1234); if (!ptr) return NULL; // ... -}{#end_syntax_block#} +} + {#end_syntax_block#}Zig code
{#syntax_block|zig|call_malloc_from_zig.zig#} // malloc prototype included for reference @@ -6072,7 +6074,8 @@ fn doAThing() ?*Foo {The other form of checking against NULL you might see looks like this:
- {#syntax_block|c|checking_null_in_c.c#}void do_a_thing(struct Foo *foo) { + {#syntax_block|c|checking_null_in_c.c#} +void do_a_thing(struct Foo *foo) { // do some stuff if (foo) { @@ -6080,7 +6083,8 @@ fn doAThing() ?*Foo { } // do some stuff -}{#end_syntax_block#} +} + {#end_syntax_block#}In Zig you can accomplish the same thing:
@@ -7443,7 +7447,8 @@ pub fn syscall3(number: usize, arg1: usize, arg2: usize, arg3: usize) usize {Dissecting the syntax:
- {#syntax_block|zig|Assembly Syntax Explained#}// Inline assembly is an expression which returns a value. + {#syntax_block|zig|Assembly Syntax Explained#} +// Inline assembly is an expression which returns a value. // the `asm` keyword begins the expression. _ = asm // `volatile` is an optional modifier that tells Zig this @@ -7498,7 +7503,8 @@ volatile ( // output. In this example we list $rcx and $r11 because it is known the // kernel syscall does not preserve these registers. : "rcx", "r11" -);{#end_syntax_block#} +); + {#end_syntax_block#}For x86 and x86_64 targets, the syntax is AT&T syntax, rather than the more popular Intel syntax. This is due to technical constraints; assembly parsing is @@ -10688,12 +10694,15 @@ const c = @cImport({ or -cflags could result in clang or Zig parse failures, or subtle ABI incompatibilities when linking with C code.
- {#syntax_block|c|varytarget.h#}long FOO = __LONG_MAX__;{#end_syntax_block#} + {#syntax_block|c|varytarget.h#} +long FOO = __LONG_MAX__; + {#end_syntax_block#} {#shell_samp#}$ zig translate-c -target thumb-freestanding-gnueabihf varytarget.h|grep FOO pub export var FOO: c_long = 2147483647; $ zig translate-c -target x86_64-macos-gnu varytarget.h|grep FOO pub export var FOO: c_long = 9223372036854775807;{#end_shell_samp#} - {#syntax_block|c|varycflags.h#}enum FOO { BAR }; + {#syntax_block|c|varycflags.h#} +enum FOO { BAR }; int do_something(enum FOO foo); {#end_syntax_block#} {#shell_samp#}$ zig translate-c varycflags.h|grep -B1 do_something @@ -10784,7 +10793,8 @@ pub fn main() void { Zig.Consider the following example:
- {#syntax_block|c|macro.c#}#define MAKELOCAL(NAME, INIT) int NAME = INIT + {#syntax_block|c|macro.c#} +#define MAKELOCAL(NAME, INIT) int NAME = INIT int foo(void) { MAKELOCAL(a, 1); MAKELOCAL(b, 2); @@ -10905,7 +10915,8 @@ export fn add(a: i32, b: i32) i32 {To make a shared library:
{#shell_samp#}$ zig build-lib mathtest.zig -dynamic{#end_shell_samp#}Here is an example with the {#link|Zig Build System#}:
- {#syntax_block|c|test.c#}// This header is generated by zig from mathtest.zig + {#syntax_block|c|test.c#} +// This header is generated by zig from mathtest.zig #include "mathtest.h" #include