7. Printing and String Interpolation
print, println, eprint and eprintln are functions of std/io.zc, so a program that prints imports it. Each one takes a single string.
| Function | Description |
|---|---|
print(s) | Prints to stdout without a trailing newline. |
println(s) | Prints to stdout with a trailing newline. |
eprint(s) | Prints to stderr without a trailing newline. |
eprintln(s) | Prints to stderr with a trailing newline. |
import "std/io.zc"
fn main() {
print("Loading... ");
println("done");
eprintln("warning: no config file");
}
String Interpolation
Only strings prefixed with f interpolate: in f"...", each expression between {} is replaced by its value. In a plain "...", a {expr} is a compile error that points to f"...".
let x = 42;
let name = "Zen";
println(f"Value: , Name: ");
Escaping Braces: inside f"...", use {{ to produce a literal { and }} for a literal }:
let json = f"JSON:
Raw Strings: To define a string where interpolation and escape sequences are completely ignored, prefix it with r (e.g., r"..."):
let regex = r"\w+"; // Contains exactly \ w +
let raw_json = r'{"key": "value"}'; // No brace escaping needed
Multiline Strings
Zen C supports raw multiline string blocks using the """ delimiter. This is extremely useful for writing embedded languages (GLSL, HTML) or generating C-code in comptime blocks without manually escaping newlines and interior quotes.
Multiline strings follow the same rules as standard strings: a plain """...""" block does not interpolate, and a {expr} in it is an error. A prefix changes that:
f"""...""": interpolates{expr}, likef"...".r"""...""": a raw block (no escape sequences).
let prompt = """
Please enter your name:
Type "exit" to cancel.
""";
let world = "world";
let script = f"""
fn hello() }
""";
let pure_raw = r"""
Here are just text, and \n is literally slash-n.
""";
Reading Input
readln() from std/io.zc reads a line from stdin, without the newline. It returns NULL at the end of the input, and the caller frees the line.
import "std/io.zc"
fn main() {
print("How old are you? ");
let line = readln();
if line != NULL {
let age = atoi(line);
println(f"You are years old.");
free(line);
}
}