summaryrefslogtreecommitdiff
path: root/examples/foreach.c
blob: 628c9934b29366889cfc144a84e29e929091be59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#define NONSTD_IMPLEMENTATION
#include "../nonstd.h"

#include <stdio.h>

int main(void) {
	// Example 1: Iterate over an array of integers
	int numbers[] = {10, 20, 30, 40, 50};
	int num; // Declare the loop variable

	static_foreach(int, num, numbers) { printf("  Number: %d\n", num); }
	printf("\n");

	// Example 2: Iterate over an array of floats
	float prices[] = {9.99f, 19.99f, 29.99f, 49.99f};
	float price; // Declare the loop variable

	static_foreach(float, price, prices) { printf("  Price: $%.2f\n", price); }
	printf("\n");

	// Example 3: Iterate over an array of strings
	const char *fruits[] = {"Apple", "Banana", "Cherry", "Date", "Elderberry"};
	const char *fruit; // Declare the loop variable

	static_foreach(const char *, fruit, fruits) { printf("  Fruit: %s\n", fruit); }
	printf("\n");

	// Example 4: Perform calculations with static_foreach
	int values[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
	int sum = 0;
	int val; // Declare the loop variable

	static_foreach(int, val, values) { sum += val; }
	printf("  Sum of values: %d\n", sum);
	printf("\n");

	// Example 5: Iterate over struct array
	typedef struct {
		const char *name;
		int age;
	} Person;

	Person people[] = {{"Alice", 25}, {"Bob", 30}, {"Charlie", 35}};
	Person person; // Declare the loop variable

	static_foreach(Person, person, people) { printf("  %s is %d years old\n", person.name, person.age); }
	printf("\n");

	// Example 6: Modify array elements in place
	int nums[] = {1, 2, 3, 4, 5};
	int n; // Declare the loop variable

	printf("  Before: ");
	static_foreach(int, n, nums) { printf("%d ", n); }
	printf("\n");

	// Note: static_foreach creates a copy of each element by default
	// To modify elements, use a traditional for loop with array indexing
	for (size_t i = 0; i < countof(nums); i++) {
		nums[i] *= 2;
	}

	printf("  After doubling: ");
	static_foreach(int, n, nums) { printf("%d ", n); }
	printf("\n");

	return 0;
}