1#define NONSTD_IMPLEMENTATION
2#include "../nonstd.h"
3
4#include <stdio.h>
5
6int main(void) {
7 // Example 1: Iterate over an array of integers
8 int numbers[] = {10, 20, 30, 40, 50};
9 int num; // Declare the loop variable
10
11 static_foreach(int, num, numbers) {
12 printf(" Number: %d\n", num);
13 }
14 printf("\n");
15
16 // Example 2: Iterate over an array of floats
17 float prices[] = {9.99f, 19.99f, 29.99f, 49.99f};
18 float price; // Declare the loop variable
19
20 static_foreach(float, price, prices) {
21 printf(" Price: $%.2f\n", price);
22 }
23 printf("\n");
24
25 // Example 3: Iterate over an array of strings
26 const char *fruits[] = {"Apple", "Banana", "Cherry", "Date", "Elderberry"};
27 const char *fruit; // Declare the loop variable
28
29 static_foreach(const char *, fruit, fruits) {
30 printf(" Fruit: %s\n", fruit);
31 }
32 printf("\n");
33
34 // Example 4: Perform calculations with static_foreach
35 int values[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
36 int sum = 0;
37 int val; // Declare the loop variable
38
39 static_foreach(int, val, values) {
40 sum += val;
41 }
42 printf(" Sum of values: %d\n", sum);
43 printf("\n");
44
45 // Example 5: Iterate over struct array
46 typedef struct {
47 const char *name;
48 int age;
49 } Person;
50
51 Person people[] = {{"Alice", 25}, {"Bob", 30}, {"Charlie", 35}};
52 Person person; // Declare the loop variable
53
54 static_foreach(Person, person, people) {
55 printf(" %s is %d years old\n", person.name, person.age);
56 }
57 printf("\n");
58
59 // Example 6: Modify array elements in place
60 int nums[] = {1, 2, 3, 4, 5};
61 int n; // Declare the loop variable
62
63 printf(" Before: ");
64 static_foreach(int, n, nums) {
65 printf("%d ", n);
66 }
67 printf("\n");
68
69 // Note: static_foreach creates a copy of each element by default
70 // To modify elements, use a traditional for loop with array indexing
71 for (size_t i = 0; i < countof(nums); i++) {
72 nums[i] *= 2;
73 }
74
75 printf(" After doubling: ");
76 static_foreach(int, n, nums) {
77 printf("%d ", n);
78 }
79 printf("\n");
80
81 return 0;
82}