Skip to content

Print & Output

Print & Output

The print() function is a built-in that outputs values to stdout. It automatically handles different types.

Basic Usage

hello.tg
func main() -> i32:
print("Hello Self-Hosted World!")
return 0
Output: Hello Self-Hosted World!

Printing Different Types

print() can handle integers, floats, and strings:

print_types.tg
# Print a string
print("Thagore is awesome")
# Print an integer
let x = 42
print(x)
# Print a float
let pi = 3.14159
print(pi)
# Print a computed expression
let a = 10
let b = 32
print(a + b)

String Concatenation for Output

Use + to concatenate strings for more complex output:

string_concat_output.tg
func main() -> i32:
let name = "Developer"
let greeting = "Hello, " + name + "!"
print(greeting)
return 0
Output: Hello, Developer!

Printing Struct Fields

Access struct fields with . notation and print them individually:

print_struct.tg
struct Point:
x: i32
y: i32
let p = Point(10, 20)
print(p.x) # Output: 10
print(p.y) # Output: 20

Using extern func printf for Formatted Output

formatted.tg
extern func printf(fmt: String, value: i32) -> i32
func main() -> i32:
printf("The answer is %d\n", 42)
return 0

Top-Level Print

Print statements can appear at the top level (outside main):

top_level.tg
let message = "Hello from top level!"
print(message)

The compiler wraps top-level statements in an auto-generated main function.