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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>

#define declare_array(name, start, size) \
    int name[size]; \
    for (int i = 0; i < size; i++) { \
        name[i] = start + i; \
    }

// this is a bit of a hack, but its the only way
// to solve this problem in the processor
#define print_array(name) do { \
    int array_size = sizeof(name) / sizeof(name[0]); \
    for (int i = 0; i < array_size; i++) { \
        printf("* %d\n", name[i]); \
    } \
} while(false)

int main(int argc, char* argv[]) {
    declare_array(numbers, 3, 10);
    declare_array(larger_numbers, 6, 15);
    
    print_array(numbers);
    print_array(larger_numbers);

    // this works with blocks
    if (numbers[0] < 5) {
        print_array(numbers);
    } else {
        printf("Larger than 5\n");
    }

    // this now works with blocks
    if (numbers[0] < 5)
        print_array(numbers);
    else
        printf("Larger than 5\n");

    return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */

© Copyright 2024 Alexander J. Quinn & David Burnett         This content is protected and may not be shared, uploaded, or distributed.