Values

    1. $ ./values
    2. 1 + 1 = 2
    3. 7.0 / 3.0 = 2.33333325e+00
    4. false
    5. true
    6. false
    7. optional 1
    8. type: ?[]const u8
    9. value: null
    10. optional 2
    11. type: ?[]const u8
    12. value: hi
    13. error union 1
    14. type: anyerror!i32
    15. value: error.ArgNotFound
    16. error union 2
    17. type: anyerror!i32
    18. value: 1234

    In addition to the integer types above, arbitrary bit-width integers can be referenced by using an identifier of i or u followed by digits. For example, the identifier i7 refers to a signed 7-bit integer. The maximum allowed bit-width of an integer type is 65535.

    See also:

    See also:

    String literals are UTF-8 encoded byte arrays.

    Character literals have type comptime_int, the same as . All Escape Sequences are valid in both string literals and character literals. Once is implemented, character literals will be allowed to have a single UTF-8 encoded codepoint.

    test.zig

    1. const assert = @import("std").debug.assert;
    2. const mem = @import("std").mem;
    3. test "string literals" {
    4. // In Zig a string literal is an array of bytes.
    5. const normal_bytes = "hello";
    6. assert(@typeOf(normal_bytes) == [5]u8);
    7. assert(normal_bytes.len == 5);
    8. assert(normal_bytes[1] == 'e');
    9. assert('e' == '\x65');
    10. assert('\U01f4a9' == 128169);
    11. assert(mem.eql(u8, "hello", "h\x65llo"));
    12. // A C string literal is a null terminated pointer.
    13. const null_terminated_bytes = c"hello";
    14. assert(@typeOf(null_terminated_bytes) == [*]const u8);
    15. assert(null_terminated_bytes[5] == 0);
    16. }
    1. $ zig test test.zig
    2. Test 1/1 string literals...OK
    3. All tests passed.

    See also:

    Multiline string literals have no escapes and can span across multiple lines. To start a multiline string literal, use the \ 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 \ then a newline is appended and the string literal continues.

    For a multiline C string literal, prepend c to each \:

    1. const c_string_literal =
    2. c\\#include <stdio.h>
    3. c\\
    4. c\\int main(int argc, char **argv) {
    5. c\\ printf("hello world\n");
    6. c\\ return 0;
    7. c\\}
    8. ;

    In this example the variable c_string_literal has type [*]const u8 and has a terminating null byte.

    See also:

    Use the const keyword to assign a value to an identifier:

    test.zig

    1. const x = 1234;
    2. fn foo() void {
    3. // It works at global scope as well as inside functions.
    4. const y = 5678;
    5. // Once assigned, an identifier cannot be changed.
    6. y += 1;
    7. }
    8. test "assignment" {
    9. foo();
    10. }
    1. $ zig test test.zig
    2. /home/andy/dev/zig/docgen_tmp/test.zig:8:7: error: cannot assign to constant
    3. y += 1;
    4. ^

    const applies to all of the bytes that the identifier immediately addresses. have their own const-ness.

    test.zig

    1. $ zig test test.zig
    2. Test 1/1 var...OK
    3. All tests passed.

    Variables must be initialized:

    test.zig

    1. test "initialization" {
    2. var x: i32;
    3. x = 1;
    4. }
    1. $ zig test test.zig
    2. /home/andy/dev/zig/docgen_tmp/test.zig:2:5: error: variables must be initialized
    3. var x: i32;
    4. ^
    5. /home/andy/dev/zig/docgen_tmp/test.zig:4:5: error: use of undeclared identifier 'x'
    6. x = 1;
    7. ^

    Use undefined to leave variables uninitialized:

    test.zig

    1. $ zig test test.zig
    2. Test 1/1 init with undefined...OK

    undefined can be implicitly cast to any type. Once this happens, it is no longer possible to detect that the value is undefined. undefined means the value could be anything, even something that is nonsense according to the type. Translated into English, undefined means "Not a meaningful value. Using this value would be a bug. The value will be unused, or overwritten before being used."

    In mode, Zig writes 0xaa bytes to undefined memory. This is to catch bugs early, and to help detect use of undefined memory in a debugger.