-
Notifications
You must be signed in to change notification settings - Fork 178
/
index.html
executable file
·6965 lines (6050 loc) · 248 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Documentation - The Zig Programming Language</title>
<style type="text/css">
.hljs{display:block;overflow-x:auto;padding:0.5em;color:#333;background:#f8f8f8}.hljs-comment,.hljs-quote{color:#998;font-style:italic}.hljs-keyword,.hljs-selector-tag,.hljs-subst{color:#333;font-weight:bold}.hljs-number,.hljs-literal,.hljs-variable,.hljs-template-variable,.hljs-tag .hljs-attr{color:#008080}.hljs-string,.hljs-doctag{color:#d14}.hljs-title,.hljs-section,.hljs-selector-id{color:#900;font-weight:bold}.hljs-subst{font-weight:normal}.hljs-type,.hljs-class .hljs-title{color:#458;font-weight:bold}.hljs-tag,.hljs-name,.hljs-attribute{color:#000080;font-weight:normal}.hljs-regexp,.hljs-link{color:#009926}.hljs-symbol,.hljs-bullet{color:#990073}.hljs-built_in,.hljs-builtin-name{color:#0086b3}.hljs-meta{color:#999;font-weight:bold}.hljs-deletion{background:#fdd}.hljs-addition{background:#dfd}.hljs-emphasis{font-style:italic}.hljs-strong{font-weight:bold}
</style>
<style type="text/css">
table, th, td {
border-collapse: collapse;
border: 1px solid grey;
}
th, td {
padding: 0.1em;
}
.t0_1, .t37, .t37_1 {
font-weight: bold;
}
.t2_0 {
color: grey;
}
.t31_1 {
color: red;
}
.t32_1 {
color: green;
}
.t36_1 {
color: #0086b3;
}
.file {
text-decoration: underline;
}
code {
font-size: 12pt;
}
pre > code {
display: block;
overflow: auto;
}
.table-wrapper {
width: 100%;
overflow-y: auto;
}
/* Desktop */
@media screen and (min-width: 56.25em) {
#nav {
width: 20em;
height: 100%;
position: fixed;
overflow-y: scroll;
left: 0;
top: 2em;
padding-left: 1em;
}
#contents {
max-width: 60em;
padding-left: 22em;
padding: 1em;
padding-left: 24em;
}
}
/* Mobile */
@media screen and (max-width: 56.25em) {
body, code {
font-size: small;
}
#nav {
border-bottom: 1px solid grey;
}
}
</style>
</head>
<body>
{#embed|vernav#}
<div id="nav">
<h3>Index</h3>
<ul>
<li><a href="#Introduction">Introduction</a></li>
<li><a href="#Hello-World">Hello World</a></li>
<li><a href="#Values">Values</a>
<ul>
<li><a href="#Primitive-Types">Primitive Types</a></li>
<li><a href="#Primitive-Values">Primitive Values</a></li>
<li><a href="#String-Literals">String Literals</a>
<ul>
<li><a href="#Escape-Sequences">Escape Sequences</a></li>
<li><a href="#Multiline-String-Literals">Multiline String Literals</a></li>
</ul></li>
<li><a href="#Assignment">Assignment</a></li>
</ul></li>
<li><a href="#Integers">Integers</a>
<ul>
<li><a href="#Integer-Literals">Integer Literals</a></li>
<li><a href="#Runtime-Integer-Values">Runtime Integer Values</a></li>
</ul></li>
<li><a href="#Floats">Floats</a>
<ul>
<li><a href="#Float-Literals">Float Literals</a></li>
<li><a href="#Floating-Point-Operations">Floating Point Operations</a></li>
</ul></li>
<li><a href="#Operators">Operators</a>
<ul>
<li><a href="#Table-of-Operators">Table of Operators</a></li>
<li><a href="#Precedence">Precedence</a></li>
</ul></li>
<li><a href="#Arrays">Arrays</a></li>
<li><a href="#Pointers">Pointers</a>
<ul>
<li><a href="#Alignment">Alignment</a></li>
<li><a href="#Type-Based-Alias-Analysis">Type Based Alias Analysis</a></li>
</ul></li>
<li><a href="#Slices">Slices</a></li>
<li><a href="#struct">struct</a></li>
<li><a href="#enum">enum</a>
<ul>
<li><a href="#extern-enum">extern enum</a></li>
</ul></li>
<li><a href="#union">union</a></li>
<li><a href="#switch">switch</a></li>
<li><a href="#while">while</a></li>
<li><a href="#for">for</a></li>
<li><a href="#if">if</a></li>
<li><a href="#defer">defer</a></li>
<li><a href="#unreachable">unreachable</a>
<ul>
<li><a href="#Basics">Basics</a></li>
<li><a href="#At-Compile-Time">At Compile-Time</a></li>
</ul></li>
<li><a href="#noreturn">noreturn</a></li>
<li><a href="#Functions">Functions</a>
<ul>
<li><a href="#Pass-by-value-Parameters">Pass-by-value Parameters</a></li>
<li><a href="#Function-Reflection">Function Reflection</a></li>
</ul></li>
<li><a href="#Errors">Errors</a>
<ul>
<li><a href="#Error-Set-Type">Error Set Type</a>
<ul>
<li><a href="#The-Global-Error-Set">The Global Error Set</a></li>
</ul></li>
<li><a href="#Error-Union-Type">Error Union Type</a>
<ul>
<li><a href="#Inferred-Error-Sets">Inferred Error Sets</a></li>
</ul></li>
<li><a href="#Error-Return-Traces">Error Return Traces</a></li>
</ul></li>
<li><a href="#Nullables">Nullables</a>
<ul>
<li><a href="#Nullable-Type">Nullable Type</a></li>
</ul></li>
<li><a href="#Casting">Casting</a></li>
<li><a href="#void">void</a></li>
<li><a href="#this">this</a></li>
<li><a href="#comptime">comptime</a>
<ul>
<li><a href="#Introducing-the-Compile-Time-Concept">Introducing the Compile-Time Concept</a>
<ul>
<li><a href="#Compile-Time-Parameters">Compile-Time Parameters</a></li>
<li><a href="#Compile-Time-Variables">Compile-Time Variables</a></li>
<li><a href="#Compile-Time-Expressions">Compile-Time Expressions</a></li>
</ul></li>
<li><a href="#Generic-Data-Structures">Generic Data Structures</a></li>
<li><a href="#Case-Study-printf-in-Zig">Case Study: printf in Zig</a></li>
</ul></li>
<li><a href="#inline">inline</a></li>
<li><a href="#Assembly">Assembly</a></li>
<li><a href="#Atomics">Atomics</a></li>
<li><a href="#Builtin-Functions">Builtin Functions</a>
<ul>
<li><a href="#addWithOverflow">@addWithOverflow</a></li>
<li><a href="#ArgType">@ArgType</a></li>
<li><a href="#atomicRmw">@atomicRmw</a></li>
<li><a href="#bitCast">@bitCast</a></li>
<li><a href="#breakpoint">@breakpoint</a></li>
<li><a href="#alignCast">@alignCast</a></li>
<li><a href="#alignOf">@alignOf</a></li>
<li><a href="#cDefine">@cDefine</a></li>
<li><a href="#cImport">@cImport</a></li>
<li><a href="#cInclude">@cInclude</a></li>
<li><a href="#cUndef">@cUndef</a></li>
<li><a href="#canImplicitCast">@canImplicitCast</a></li>
<li><a href="#clz">@clz</a></li>
<li><a href="#cmpxchg">@cmpxchg</a></li>
<li><a href="#compileError">@compileError</a></li>
<li><a href="#compileLog">@compileLog</a></li>
<li><a href="#ctz">@ctz</a></li>
<li><a href="#divExact">@divExact</a></li>
<li><a href="#divFloor">@divFloor</a></li>
<li><a href="#divTrunc">@divTrunc</a></li>
<li><a href="#embedFile">@embedFile</a></li>
<li><a href="#export">@export</a></li>
<li><a href="#tagName">@tagName</a></li>
<li><a href="#TagType">@TagType</a></li>
<li><a href="#errorName">@errorName</a></li>
<li><a href="#errorReturnTrace">@errorReturnTrace</a></li>
<li><a href="#fence">@fence</a></li>
<li><a href="#fieldParentPtr">@fieldParentPtr</a></li>
<li><a href="#frameAddress">@frameAddress</a></li>
<li><a href="#import">@import</a></li>
<li><a href="#inlineCall">@inlineCall</a></li>
<li><a href="#intToPtr">@intToPtr</a></li>
<li><a href="#IntType">@IntType</a></li>
<li><a href="#maxValue">@maxValue</a></li>
<li><a href="#memberCount">@memberCount</a></li>
<li><a href="#memberName">@memberName</a></li>
<li><a href="#memberType">@memberType</a></li>
<li><a href="#memcpy">@memcpy</a></li>
<li><a href="#memset">@memset</a></li>
<li><a href="#minValue">@minValue</a></li>
<li><a href="#mod">@mod</a></li>
<li><a href="#mulWithOverflow">@mulWithOverflow</a></li>
<li><a href="#noInlineCall">@noInlineCall</a></li>
<li><a href="#offsetOf">@offsetOf</a></li>
<li><a href="#OpaqueType">@OpaqueType</a></li>
<li><a href="#panic">@panic</a></li>
<li><a href="#ptrCast">@ptrCast</a></li>
<li><a href="#ptrToInt">@ptrToInt</a></li>
<li><a href="#rem">@rem</a></li>
<li><a href="#returnAddress">@returnAddress</a></li>
<li><a href="#setAlignStack">@setAlignStack</a></li>
<li><a href="#setCold">@setCold</a></li>
<li><a href="#setRuntimeSafety">@setRuntimeSafety</a></li>
<li><a href="#setEvalBranchQuota">@setEvalBranchQuota</a></li>
<li><a href="#setFloatMode">@setFloatMode</a></li>
<li><a href="#setGlobalLinkage">@setGlobalLinkage</a></li>
<li><a href="#setGlobalSection">@setGlobalSection</a></li>
<li><a href="#shlExact">@shlExact</a></li>
<li><a href="#shlWithOverflow">@shlWithOverflow</a></li>
<li><a href="#shrExact">@shrExact</a></li>
<li><a href="#sizeOf">@sizeOf</a></li>
<li><a href="#subWithOverflow">@subWithOverflow</a></li>
<li><a href="#truncate">@truncate</a></li>
<li><a href="#typeId">@typeId</a></li>
<li><a href="#typeName">@typeName</a></li>
<li><a href="#typeOf">@typeOf</a></li>
</ul></li>
<li><a href="#Build-Mode">Build Mode</a>
<ul>
<li><a href="#Debug">Debug</a></li>
<li><a href="#ReleaseFast">ReleaseFast</a></li>
<li><a href="#ReleaseSafe">ReleaseSafe</a></li>
</ul></li>
<li><a href="#Undefined-Behavior">Undefined Behavior</a>
<ul>
<li><a href="#Reaching-Unreachable-Code">Reaching Unreachable Code</a></li>
<li><a href="#Index-out-of-Bounds">Index out of Bounds</a></li>
<li><a href="#Cast-Negative-Number-to-Unsigned-Integer">Cast Negative Number to Unsigned Integer</a></li>
<li><a href="#Cast-Truncates-Data">Cast Truncates Data</a></li>
<li><a href="#Integer-Overflow">Integer Overflow</a>
<ul>
<li><a href="#Default-Operations">Default Operations</a></li>
<li><a href="#Standard-Library-Math-Functions">Standard Library Math Functions</a></li>
<li><a href="#Builtin-Overflow-Functions">Builtin Overflow Functions</a></li>
<li><a href="#Wrapping-Operations">Wrapping Operations</a></li>
</ul></li>
<li><a href="#Exact-Left-Shift-Overflow">Exact Left Shift Overflow</a></li>
<li><a href="#Exact-Right-Shift-Overflow">Exact Right Shift Overflow</a></li>
<li><a href="#Division-by-Zero">Division by Zero</a></li>
<li><a href="#Remainder-Division-by-Zero">Remainder Division by Zero</a></li>
<li><a href="#Exact-Division-Remainder">Exact Division Remainder</a></li>
<li><a href="#Slice-Widen-Remainder">Slice Widen Remainder</a></li>
<li><a href="#Attempt-to-Unwrap-Null">Attempt to Unwrap Null</a></li>
<li><a href="#Attempt-to-Unwrap-Error">Attempt to Unwrap Error</a></li>
<li><a href="#Invalid-Error-Code">Invalid Error Code</a></li>
<li><a href="#Invalid-Enum-Cast">Invalid Enum Cast</a></li>
<li><a href="#Incorrect-Pointer-Alignment">Incorrect Pointer Alignment</a></li>
<li><a href="#Wrong-Union-Field-Access">Wrong Union Field Access</a></li>
</ul></li>
<li><a href="#Memory">Memory</a></li>
<li><a href="#Compile-Variables">Compile Variables</a></li>
<li><a href="#Root-Source-File">Root Source File</a></li>
<li><a href="#Zig-Test">Zig Test</a></li>
<li><a href="#Zig-Build-System">Zig Build System</a></li>
<li><a href="#C">C</a>
<ul>
<li><a href="#C-Type-Primitives">C Type Primitives</a></li>
<li><a href="#C-String-Literals">C String Literals</a></li>
<li><a href="#Import-from-C-Header-File">Import from C Header File</a></li>
<li><a href="#Mixing-Object-Files">Mixing Object Files</a></li>
<li><a href="#Terminal">Terminal</a></li>
</ul></li>
<li><a href="#Targets">Targets</a></li>
<li><a href="#Style-Guide">Style Guide</a>
<ul>
<li><a href="#Whitespace">Whitespace</a></li>
<li><a href="#Names">Names</a></li>
<li><a href="#Examples">Examples</a></li>
</ul></li>
<li><a href="#Source-Encoding">Source Encoding</a></li>
<li><a href="#Grammar">Grammar</a></li>
<li><a href="#Zen">Zen</a></li>
</ul>
</div>
<div id="contents">
<h1 id="Introduction">Introduction</h1>
<p>
Zig is an open-source programming language designed for <strong>robustness</strong>,
<strong>optimality</strong>, and <strong>clarity</strong>.
</p>
<ul>
<li><strong>Robust</strong> - behavior is correct even for edge cases such as out of memory.</li>
<li><strong>Optimal</strong> - write programs the best way they can behave and perform.</li>
<li><strong>Clear</strong> - precisely communicate your intent to the compiler and other programmers. The language imposes a low overhead to reading code.</li>
</ul>
<p>
Often the most efficient way to learn something new is to see examples, so
this documentation shows how to use each of Zig's features. It is
all on one page so you can search with your browser's search tool.
</p>
<p>
The code samples in this document are compiled and tested as part of the main test suite of Zig.
This HTML document depends on no external files, so you can use it offline.
</p>
<h1 id="Hello-World">Hello World</h1>
<p class="file">hello.zig</p><pre><code class="zig">const std = @import("std");
pub fn main() !void {
// If this program is run without stdout attached, exit with an error.
var 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");
}</code></pre><pre><code class="shell">$ zig build-exe hello.zig
$ ./hello
Hello, world!
</code></pre>
<p>
Usually you don't want to write to stdout. You want to write to stderr. And you
don't care if it fails. It's more like a <em>warning message</em> that you want
to emit. For that you can use a simpler API:
</p>
<p class="file">hello.zig</p><pre><code class="zig">const warn = @import("std").debug.warn;
pub fn main() void {
warn("Hello, world!\n");
}</code></pre><pre><code class="shell">$ zig build-exe hello.zig
$ ./hello
Hello, world!
</code></pre>
<p>
Note that we also left off the <code class="zig">!</code> from the return type.
In Zig, if your main function cannot fail, you must use the <code class="zig">void</code> return type.
</p>
<p>See also:</p><ul>
<li><a href="#Values">Values</a></li>
<li><a href="#import">@import</a></li>
<li><a href="#Errors">Errors</a></li>
<li><a href="#Root-Source-File">Root Source File</a></li>
</ul>
<h1 id="Values">Values</h1>
<p class="file">values.zig</p><pre><code class="zig">const std = @import("std");
const warn = std.debug.warn;
const os = std.os;
const assert = std.debug.assert;
pub fn main() void {
// integers
const one_plus_one: i32 = 1 + 1;
warn("1 + 1 = {}\n", one_plus_one);
// floats
const seven_div_three: f32 = 7.0 / 3.0;
warn("7.0 / 3.0 = {}\n", seven_div_three);
// boolean
warn("{}\n{}\n{}\n",
true and false,
true or false,
!true);
// nullable
var nullable_value: ?[]const u8 = null;
assert(nullable_value == null);
warn("\nnullable 1\ntype: {}\nvalue: {}\n",
@typeName(@typeOf(nullable_value)), nullable_value);
nullable_value = "hi";
assert(nullable_value != null);
warn("\nnullable 2\ntype: {}\nvalue: {}\n",
@typeName(@typeOf(nullable_value)), nullable_value);
// error union
var number_or_error: error!i32 = error.ArgNotFound;
warn("\nerror union 1\ntype: {}\nvalue: {}\n",
@typeName(@typeOf(number_or_error)), number_or_error);
number_or_error = 1234;
warn("\nerror union 2\ntype: {}\nvalue: {}\n",
@typeName(@typeOf(number_or_error)), number_or_error);
}</code></pre><pre><code class="shell">$ zig build-exe values.zig
$ ./values
1 + 1 = 2
7.0 / 3.0 = 2.33333325
false
true
false
nullable 1
type: ?[]const u8
value: null
nullable 2
type: ?[]const u8
value: hi
error union 1
type: error!i32
value: error.ArgNotFound
error union 2
type: error!i32
value: 1234
</code></pre>
<h2 id="Primitive-Types">Primitive Types</h2>
<div class="table-wrapper">
<table>
<tr>
<th>
Name
</th>
<th>
C Equivalent
</th>
<th>
Description
</th>
</tr>
<tr>
<td><code>i2</code></td>
<td><code>(none)</code></td>
<td>signed 2-bit integer</td>
</tr>
<tr>
<td><code>u2</code></td>
<td><code>(none)</code></td>
<td>unsigned 2-bit integer</td>
</tr>
<tr>
<td><code>i3</code></td>
<td><code>(none)</code></td>
<td>signed 3-bit integer</td>
</tr>
<tr>
<td><code>u3</code></td>
<td><code>(none)</code></td>
<td>unsigned 3-bit integer</td>
</tr>
<tr>
<td><code>i4</code></td>
<td><code>(none)</code></td>
<td>signed 4-bit integer</td>
</tr>
<tr>
<td><code>u4</code></td>
<td><code>(none)</code></td>
<td>unsigned 4-bit integer</td>
</tr>
<tr>
<td><code>i5</code></td>
<td><code>(none)</code></td>
<td>signed 5-bit integer</td>
</tr>
<tr>
<td><code>u5</code></td>
<td><code>(none)</code></td>
<td>unsigned 5-bit integer</td>
</tr>
<tr>
<td><code>i6</code></td>
<td><code>(none)</code></td>
<td>signed 6-bit integer</td>
</tr>
<tr>
<td><code>u6</code></td>
<td><code>(none)</code></td>
<td>unsigned 6-bit integer</td>
</tr>
<tr>
<td><code>i7</code></td>
<td><code>(none)</code></td>
<td>signed 7-bit integer</td>
</tr>
<tr>
<td><code>u7</code></td>
<td><code>(none)</code></td>
<td>unsigned 7-bit integer</td>
</tr>
<tr>
<td><code>i8</code></td>
<td><code>int8_t</code></td>
<td>signed 8-bit integer</td>
</tr>
<tr>
<td><code>u8</code></td>
<td><code>uint8_t</code></td>
<td>unsigned 8-bit integer</td>
</tr>
<tr>
<td><code>i16</code></td>
<td><code>int16_t</code></td>
<td>signed 16-bit integer</td>
</tr>
<tr>
<td><code>u16</code></td>
<td><code>uint16_t</code></td>
<td>unsigned 16-bit integer</td>
</tr>
<tr>
<td><code>i32</code></td>
<td><code>int32_t</code></td>
<td>signed 32-bit integer</td>
</tr>
<tr>
<td><code>u32</code></td>
<td><code>uint32_t</code></td>
<td>unsigned 32-bit integer</td>
</tr>
<tr>
<td><code>i64</code></td>
<td><code>int64_t</code></td>
<td>signed 64-bit integer</td>
</tr>
<tr>
<td><code>u64</code></td>
<td><code>uint64_t</code></td>
<td>unsigned 64-bit integer</td>
</tr>
<tr>
<td><code>i128</code></td>
<td><code>__int128</code></td>
<td>signed 128-bit integer</td>
</tr>
<tr>
<td><code>u128</code></td>
<td><code>unsigned __int128</code></td>
<td>unsigned 128-bit integer</td>
</tr>
<tr>
<td><code>isize</code></td>
<td><code>intptr_t</code></td>
<td>signed pointer sized integer</td>
</tr>
<tr>
<td><code>usize</code></td>
<td><code>uintptr_t</code></td>
<td>unsigned pointer sized integer</td>
</tr>
<tr>
<td><code>c_short</code></td>
<td><code>short</code></td>
<td>for ABI compatibility with C</td>
</tr>
<tr>
<td><code>c_ushort</code></td>
<td><code>unsigned short</code></td>
<td>for ABI compatibility with C</td>
</tr>
<tr>
<td><code>c_int</code></td>
<td><code>int</code></td>
<td>for ABI compatibility with C</td>
</tr>
<tr>
<td><code>c_uint</code></td>
<td><code>unsigned int</code></td>
<td>for ABI compatibility with C</td>
</tr>
<tr>
<td><code>c_long</code></td>
<td><code>long</code></td>
<td>for ABI compatibility with C</td>
</tr>
<tr>
<td><code>c_ulong</code></td>
<td><code>unsigned long</code></td>
<td>for ABI compatibility with C</td>
</tr>
<tr>
<td><code>c_longlong</code></td>
<td><code>long long</code></td>
<td>for ABI compatibility with C</td>
</tr>
<tr>
<td><code>c_ulonglong</code></td>
<td><code>unsigned long long</code></td>
<td>for ABI compatibility with C</td>
</tr>
<tr>
<td><code>c_longdouble</code></td>
<td><code>long double</code></td>
<td>for ABI compatibility with C</td>
</tr>
<tr>
<td><code>c_void</code></td>
<td><code>void</code></td>
<td>for ABI compatibility with C</td>
</tr>
<tr>
<td><code>f32</code></td>
<td><code>float</code></td>
<td>32-bit floating point (23-bit mantissa)</td>
</tr>
<tr>
<td><code>f64</code></td>
<td><code>double</code></td>
<td>64-bit floating point (52-bit mantissa)</td>
</tr>
<tr>
<td><code>f128</code></td>
<td>(none)</td>
<td>128-bit floating point (112-bit mantissa)</td>
</tr>
<tr>
<td><code>bool</code></td>
<td><code>bool</code></td>
<td><code>true</code> or <code>false</code></td>
</tr>
<tr>
<td><code>void</code></td>
<td>(none)</td>
<td>0 bit type</td>
</tr>
<tr>
<td><code>noreturn</code></td>
<td>(none)</td>
<td>the type of <code>break</code>, <code>continue</code>, <code>return</code>, <code>unreachable</code>, and <code>while (true) {}</code></td>
</tr>
<tr>
<td><code>type</code></td>
<td>(none)</td>
<td>the type of types</td>
</tr>
<tr>
<td><code>error</code></td>
<td>(none)</td>
<td>an error code</td>
</tr>
</table>
</div>
<p>See also:</p><ul>
<li><a href="#Integers">Integers</a></li>
<li><a href="#Floats">Floats</a></li>
<li><a href="#void">void</a></li>
<li><a href="#Errors">Errors</a></li>
</ul>
<h2 id="Primitive-Values">Primitive Values</h2>
<div class="table-wrapper">
<table>
<tr>
<th>
Name
</th>
<th>
Description
</th>
</tr>
<tr>
<td><code>true</code> and <code>false</code></td>
<td><code>bool</code> values</td>
</tr>
<tr>
<td><code>null</code></td>
<td>used to set a nullable type to <code>null</code></td>
</tr>
<tr>
<td><code>undefined</code></td>
<td>used to leave a value unspecified</td>
</tr>
<tr>
<td><code>this</code></td>
<td>refers to the thing in immediate scope</td>
</tr>
</table>
</div>
<p>See also:</p><ul>
<li><a href="#Nullables">Nullables</a></li>
<li><a href="#this">this</a></li>
</ul>
<h2 id="String-Literals">String Literals</h2>
<p class="file">test.zig</p><pre><code class="zig">const assert = @import("std").debug.assert;
const mem = @import("std").mem;
test "string literals" {
// In Zig a string literal is an array of bytes.
const normal_bytes = "hello";
assert(@typeOf(normal_bytes) == [5]u8);
assert(normal_bytes.len == 5);
assert(normal_bytes[1] == 'e');
assert('e' == '\x65');
assert(mem.eql(u8, "hello", "h\x65llo"));
// A C string literal is a null terminated pointer.
const null_terminated_bytes = c"hello";
assert(@typeOf(null_terminated_bytes) == &const u8);
assert(null_terminated_bytes[5] == 0);
}</code></pre><pre><code class="shell">$ zig test test.zig
Test 1/1 string literals...OK
</code></pre>
<p>See also:</p><ul>
<li><a href="#Arrays">Arrays</a></li>
<li><a href="#Zig-Test">Zig Test</a></li>
</ul>
<h3 id="Escape-Sequences">Escape Sequences</h3>
<div class="table-wrapper">
<table>
<tr>
<th>
Escape Sequence
</th>
<th>
Name
</th>
</tr>
<tr>
<td><code>\n</code></td>
<td>Newline</td>
</tr>
<tr>
<td><code>\r</code></td>
<td>Carriage Return</td>
</tr>
<tr>
<td><code>\t</code></td>
<td>Tab</td>
</tr>
<tr>
<td><code>\\</code></td>
<td>Backslash</td>
</tr>
<tr>
<td><code>\'</code></td>
<td>Single Quote</td>
</tr>
<tr>
<td><code>\"</code></td>
<td>Double Quote</td>
</tr>
<tr>
<td><code>\xNN</code></td>
<td>hexadecimal 8-bit character code (2 digits)</td>
</tr>
<tr>
<td><code>\uNNNN</code></td>
<td>hexadecimal 16-bit Unicode character code UTF-8 encoded (4 digits)</td>
</tr>
<tr>
<td><code>\UNNNNNN</code></td>
<td>hexadecimal 24-bit Unicode character code UTF-8 encoded (6 digits)</td>
</tr>
</table>
</div>
<p>Note that the maximum valid Unicode point is <code>0x10ffff</code>.</p>
<h3 id="Multiline-String-Literals">Multiline String Literals</h3>
<p>
Multiline string literals have no escapes and can span across multiple lines.
To start a multiline string literal, use the <code>\\</code> token. Just like a comment,
the string literal goes until the end of the line. The end of the line is
not included in the string literal.
However, if the next line begins with <code>\\</code> then a newline is appended and
the string literal continues.
</p>
<pre><code class="zig">const hello_world_in_c =
\\#include <stdio.h>
\\
\\int main(int argc, char **argv) {
\\ printf("hello world\n");
\\ return 0;
\\}
;</code></pre>
<p>
For a multiline C string literal, prepend <code>c</code> to each <code>\\</code>:
</p>
<pre><code class="zig">const c_string_literal =
c\\#include <stdio.h>
c\\
c\\int main(int argc, char **argv) {
c\\ printf("hello world\n");
c\\ return 0;
c\\}
;</code></pre>
<p>
In this example the variable <code>c_string_literal</code> has type <code>&const char</code> and
has a terminating null byte.
</p>
<p>See also:</p><ul>
<li><a href="#embedFile">@embedFile</a></li>
</ul>
<h2 id="Assignment">Assignment</h2>
<p>Use <code>const</code> to assign a value to an identifier:</p>
<p class="file">test.zig</p><pre><code class="zig">const x = 1234;
fn foo() void {
// It works at global scope as well as inside functions.
const y = 5678;
// Once assigned, an identifier cannot be changed.
y += 1;
}
test "assignment" {
foo();
}</code></pre><pre><code class="shell">$ zig test test.zig
<span class="t0_1">/home/andy/dev/zig/docgen_tmp/test.zig:8:7: </span><span class="t31_1">error:</span><span class="t0_1"> cannot assign to constant</span>
y += 1;
<span class="t32_1">^</span>
</code></pre>
<p>If you need a variable that you can modify, use <code>var</code>:</p>
<p class="file">test.zig</p><pre><code class="zig">const assert = @import("std").debug.assert;
test "var" {
var y: i32 = 5678;
y += 1;
assert(y == 5679);
}</code></pre><pre><code class="shell">$ zig test test.zig
Test 1/1 var...OK
</code></pre>
<p>Variables must be initialized:</p>
<p class="file">test.zig</p><pre><code class="zig">test "initialization" {
var x: i32;
x = 1;
}</code></pre><pre><code class="shell">$ zig test test.zig
<span class="t0_1">/home/andy/dev/zig/docgen_tmp/test.zig:2:5: </span><span class="t31_1">error:</span><span class="t0_1"> variables must be initialized</span>
var x: i32;
<span class="t32_1">^</span>
<span class="t0_1">/home/andy/dev/zig/docgen_tmp/test.zig:4:5: </span><span class="t31_1">error:</span><span class="t0_1"> use of undeclared identifier 'x'</span>
x = 1;
<span class="t32_1">^</span>
</code></pre>
<p>Use <code>undefined</code> to leave variables uninitialized:</p>
<p class="file">test.zig</p><pre><code class="zig">const assert = @import("std").debug.assert;
test "init with undefined" {
var x: i32 = undefined;
x = 1;
assert(x == 1);
}</code></pre><pre><code class="shell">$ zig test test.zig
Test 1/1 init with undefined...OK
</code></pre>
<h1 id="Integers">Integers</h1>
<h2 id="Integer-Literals">Integer Literals</h2>
<pre><code class="zig">const decimal_int = 98222;
const hex_int = 0xff;
const another_hex_int = 0xFF;
const octal_int = 0o755;
const binary_int = 0b11110000;</code></pre>
<h2 id="Runtime-Integer-Values">Runtime Integer Values</h2>
<p>
Integer literals have no size limitation, and if any undefined behavior occurs,
the compiler catches it.
</p>
<p>
However, once an integer value is no longer known at compile-time, it must have a
known size, and is vulnerable to undefined behavior.
</p>
<pre><code class="zig">fn divide(a: i32, b: i32) i32 {
return a / b;
}</code></pre>
<p>
In this function, values <code>a</code> and <code>b</code> are known only at runtime,
and thus this division operation is vulnerable to both integer overflow and
division by zero.
</p>
<p>
Operators such as <code>+</code> and <code>-</code> cause undefined behavior on
integer overflow. Also available are operations such as <code>+%</code> and
<code>-%</code> which are defined to have wrapping arithmetic on all targets.
</p>
<p>See also:</p><ul>
<li><a href="#Integer-Overflow">Integer Overflow</a></li>
<li><a href="#Division-by-Zero">Division by Zero</a></li>
<li><a href="#Wrapping-Operations">Wrapping Operations</a></li>
</ul>
<h1 id="Floats">Floats</h1>
<h2 id="Float-Literals">Float Literals</h2>
<pre><code class="zig">const floating_point = 123.0E+77;
const another_float = 123.0;
const yet_another = 123.0e+77;
const hex_floating_point = 0x103.70p-5;
const another_hex_float = 0x103.70;
const yet_another_hex_float = 0x103.70P-5;</code></pre>
<h2 id="Floating-Point-Operations">Floating Point Operations</h2>
<p>By default floating point operations use <code>Optimized</code> mode,
but you can switch to <code>Strict</code> mode on a per-block basis:</p>
<p class="file">foo.zig</p><pre><code class="zig">const builtin = @import("builtin");
const big = f64(1 << 40);
export fn foo_strict(x: f64) f64 {
@setFloatMode(this, builtin.FloatMode.Strict);
return x + big - big;
}
export fn foo_optimized(x: f64) f64 {
return x + big - big;
}</code></pre><pre><code class="shell">$ zig build-obj foo.zig --release-fast</code></pre>
<p>For this test we have to separate code into two object files -
otherwise the optimizer figures out all the values at compile-time,
which operates in strict mode.</p>
<p class="file">float_mode.zig</p><pre><code class="zig">const warn = @import("std").debug.warn;
extern fn foo_strict(x: f64) f64;
extern fn foo_optimized(x: f64) f64;
pub fn main() void {
const x = 0.001;
warn("optimized = {}\n", foo_optimized(x));
warn("strict = {}\n", foo_strict(x));
}</code></pre><pre><code class="shell">$ zig build-exe float_mode.zig --object foo.o
$ ./float_mode
optimized = 1.0e-3
strict = 9.765625e-4
</code></pre>
<p>See also:</p><ul>
<li><a href="#setFloatMode">@setFloatMode</a></li>
<li><a href="#Division-by-Zero">Division by Zero</a></li>
</ul>
<h1 id="Operators">Operators</h1>
<h2 id="Table-of-Operators">Table of Operators</h2>
<div class="table-wrapper">
<table>
<tr>
<th>
Syntax
</th>
<th>
Relevant Types
</th>
<th>
Description
</th>
<th>
Example
</th>
</tr>
<tr>
<td><pre><code class="zig">a + b