2. 原始類型

類型C 等效類型描述
int, uintint32_t, uint32_t32位元有號/無號整數
c_char, c_ucharchar, unsigned charC char (互操作)
c_short, c_ushortshort, unsigned shortC short (互操作)
c_int, c_uintint, unsigned intC int (互操作)
c_long, c_ulonglong, unsigned longC long (互操作)
c_longlong, c_ulonglonglong long, unsigned long longC long long / unsigned long long (互操作)
I8 .. I128 或 i8 .. i128int8_t .. __int128_t有符號固定寬度整數
U8 .. U128 或 u8 .. u128uint8_t .. __uint128_t無符號固定寬度整數
isize, usizeptrdiff_t, size_t指針大小的整數
byteuint8_tU8 的別名
F32, F64 或 f32, f64float, double浮點數
boolbooltrue 或 false
charchar單個字符
stringchar*C-string (以 null 結尾)
U0, u0, voidvoid空類型
iN (例如 i256)_BitInt(N)任意位寬有符號整數 (C23)
uN (例如 u42)unsigned _BitInt(N)任意位寬無符號整數 (C23)
runeuint32_tUnicode 標量值 (UTF-32 碼點)

字面量

Unicode 與 Rune

Zen C 通過 rune 類型提供對 Unicode 標量值的一等支持。一個 rune 代表一個 Unicode 碼點(編碼為 32 位無符號整數)。

字面量描述
'a'標準 ASCII 字符
'🚀'多字節 Unicode 字符
'\u{2764}'Unicode 轉義序列 (十六進制)
import "std.zc"
import "std/io.zc"

fn main() {
    let c = 'a';
    println(f"字符 '{c}' 的 ASCII/Unicode 編碼為 {(int)c}");

    let code = 97;
    println(f"編碼 {code} 對應的字符為 {(char)code}");

    let r: rune = '🚀';
    println(f"Rune '{r}' 的 Unicode 編碼為 {(uint)r}");
    
    let r_code: uint = 128640;
    println(f"編碼 {r_code} 對應的 Rune 為 '{(rune)r_code}'");

    let r_esc: rune = '\u{2764}';
    println(f"Rune '{r_esc}' 的編碼為 {(uint)r_esc} (0x{(uint)r_esc:X})");
}
Important

可移植代碼最佳實踐

  • 對於所有純 Zen C 邏輯,請使用 可移植類型 (int、uint、i64、u8 等)。int 保證在所有架構上都是 32 位元有號整數。
  • 僅在與 C 庫 (FFI) 交互時使用 C 互操作類型 (c_int、c_char、c_long, c_ulong, c_longlong, c_ulonglong)。它們的大小因平台和 C 編譯器而異。
  • 使用 isize 和 usize 進行數組索引和內存指針運算。