Skip to content

Commit 038b67b

Browse files
docs: auto-sync Rosetta Code examples from zenc repo
1 parent b208423 commit 038b67b

7 files changed

Lines changed: 29 additions & 18 deletions

File tree

rosetta/Array_concatenation.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ title = "Array concatenation"
66

77
Zen C is a systems language that provides both low-level memory control and high-level standard library abstractions. Here are two idiomatic ways to concatenate collections.
88

9-
=== Using Raw Arrays ===
9+
### Using Raw Arrays
10+
1011
For fixed-size C-style arrays, concatenation is typically done by allocating a new array and using fast memory block copying via <code>memcpy</code>.
1112

1213
```zc
@@ -38,7 +39,8 @@ fn main() {
3839
Concatenated array: [1, 2, 3, 4, 5]
3940
```
4041

41-
=== Using the Standard Library ===
42+
### Using the Standard Library
43+
4244
For a more modern, ergonomic approach, Zen C's standard library provides a generic <code>Vec<T></code> type. Vectors support operator overloading, allowing them to be concatenated directly using the <code>+</code> operator.
4345

4446
```zc

rosetta/Array_length.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ title = "Array length"
66

77
Zen C provides different ways to determine the number of elements depending on whether you are using raw fixed-size arrays or standard library vectors.
88

9-
=== Using Raw Arrays ===
9+
### Using Raw Arrays
10+
1011
For raw C-style arrays, the number of elements is determined using the classic <code>sizeof</code> technique, dividing the total byte size of the array by the byte size of a single element.
1112

1213
```zc
@@ -26,7 +27,8 @@ fn main() {
2627
Raw array length: 2
2728
```
2829

29-
=== Using the Standard Library ===
30+
### Using the Standard Library
31+
3032
When using the <code>Vec<T></code> type from the standard library, determining the number of elements is as simple as calling the <code>.length()</code> method.
3133

3234
```zc

rosetta/Binary_search.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title = "Binary search"
44

55
# Binary search
66

7-
=== Iterative ===
7+
### Iterative
88

99
```zc
1010
fn binary_search_iterative(arr: int*, value: int, low: int, high: int) -> int {
@@ -51,7 +51,7 @@ Value 5 not found.
5151
Value 20 found at index 9.
5252
```
5353

54-
=== Recursive ===
54+
### Recursive
5555

5656
```zc
5757
fn binary_search_recursive(arr: int*, value: int, low: int, high: int) -> int {

rosetta/Code_Golf.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ title = "Code Golf"
44

55
# Code Golf
66

7-
=== With Literals ===
7+
### With Literals
8+
89
Zen C allows string literals to be evaluated as statements to print them to standard output. By appending the <code>..</code> operator, the implicit trailing newline is suppressed, emitting exactly the 9 required characters.
910

1011
```zc
@@ -13,7 +14,8 @@ fn main(){"Code Golf"..}
1314

1415
Length: 24 bytes.
1516

16-
=== Without Literals ===
17+
### Without Literals
18+
1719
This version assumes the compiled executable is renamed to <code>Code Golf</code> and executed via <code>./Code Golf</code>. It uses the program's own execution path in <code>argv[0]</code>, offsets the pointer by 2 to skip the <code>./</code> prefix, and uses the POSIX <code>write</code> function to emit exactly 9 bytes to stdout (file descriptor 1). Omitting the explicit declaration for <code>write</code> triggers a compiler warning but saves bytes.
1820

1921
```zc
@@ -22,7 +24,8 @@ fn main(argc:int,argv:char**){write(1,argv[0]+2,9)}
2224

2325
Length: 51 bytes.
2426

25-
=== Executable Size ===
27+
### Executable Size
28+
2629
A standard build produces an executable of around **17KB**. However, when the binary is stripped and compiled using the Tiny C Compiler (TCC) backend, the footprint can be reduced to just **8.6KB**.
2730

2831
---

rosetta/FizzBuzz.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ title = "FizzBuzz"
66

77
Zen C offers multiple ways to solve FizzBuzz. Here are two approaches: one using standard <code>if/else</code> statements and another using the <code>match</code> control flow feature.
88

9-
=== Using if/else ===
9+
### Using if/else
10+
1011
This approach demonstrates Zen C's inclusive range loops (<code>..=</code>), basic modulo arithmetic, and implicit string interpolation.
1112

1213
```zc
@@ -25,7 +26,8 @@ fn main() {
2526
}
2627
```
2728

28-
=== Using match ===
29+
### Using match
30+
2931
This implementation utilizes the <code>match</code> statement, which serves as a powerful alternative to <code>switch</code>. It supports multiple value matching on a single branch using commas, and includes a catch-all wildcard (<code>_</code>).
3032

3133
```zc

rosetta/Hello_world_Newbie.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ title = "Hello world/Newbie"
77
Zen C is a modern systems programming language that compiles to human-readable GNU C/C.<br>
88
Docs and the like can be found on it's [https://github.com/zenc-lang/zenc github] page. <br>
99

10-
===Supported Platforms and Backends===
10+
### Supported Platforms and Backends
11+
1112
The Zen C compiler, <code>zc</code>, has support for Windows, macOS and Linux. As it is a transpiler (a source-to-source compiler), it acts as a front-end, requiring a back-end. Some of the available backends are <code>gcc</code>, <code>clang</code>, <code>tcc</code>, <code>zig cc</code>. The list is not exhaustive. By default, <code>gcc</code> will be used, but you can specify the back-end with the <code>--cc</code> flag.
1213

13-
===Installation===
14-
====Compile from source====
14+
### Installation
15+
16+
#### Compile from source
1517

1618
```zc
1719
git clone https://github.com/zenc-lang/zenc.git
@@ -23,7 +25,7 @@ sudo make install
2325

2426
If you are on Windows you can build using the provided batch script, <code>build.bat</code>.
2527

26-
===Example===
28+
### Example
2729

2830
```zc
2931
fn main() {

rosetta/Repeat.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title = "Repeat"
44

55
# Repeat
66

7-
=== Standard Closures ===
7+
### Standard Closures
88

99
```zc
1010
// The procedure accepts a standard closure and an integer
@@ -29,7 +29,7 @@ Hello from closure!
2929
Hello from closure!
3030
```
3131

32-
=== State-Capturing Closures ===
32+
### State-Capturing Closures
3333

3434
```zc
3535
// Reusing the 'times' procedure defined above
@@ -52,7 +52,7 @@ Iteration 4
5252
Iteration 5
5353
```
5454

55-
=== Raw Function Pointers ===
55+
### Raw Function Pointers
5656

5757
```zc
5858
// The procedure accepts a raw C-style function pointer

0 commit comments

Comments
 (0)