Print & Output
Print & Output
The print() function is a built-in that outputs values to stdout. It automatically handles different types.
Basic Usage
func main() -> i32: print("Hello Self-Hosted World!") return 0Output: Hello Self-Hosted World!Printing Different Types
print() can handle integers, floats, and strings:
# Print a stringprint("Thagore is awesome")
# Print an integerlet x = 42print(x)
# Print a floatlet pi = 3.14159print(pi)
# Print a computed expressionlet a = 10let b = 32print(a + b)String Concatenation for Output
Use + to concatenate strings for more complex output:
func main() -> i32: let name = "Developer" let greeting = "Hello, " + name + "!" print(greeting) return 0Output: Hello, Developer!Printing Struct Fields
Access struct fields with . notation and print them individually:
struct Point: x: i32 y: i32
let p = Point(10, 20)print(p.x) # Output: 10print(p.y) # Output: 20Using extern func printf for Formatted Output
extern func printf(fmt: String, value: i32) -> i32
func main() -> i32: printf("The answer is %d\n", 42) return 0Top-Level Print
Print statements can appear at the top level (outside main):
let message = "Hello from top level!"print(message)The compiler wraps top-level statements in an auto-generated main function.