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 | #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; \
}
// variables declared in a macro exist outside the
// macro. This means they can conflict with same
// named variables
#define print_array(name) { \
int array_size = sizeof(name) / sizeof(name[0]); \
for (int i = 0; i < array_size; i++) { \
printf("* %d\n", name[i]); \
} \
}
int main(int argc, char* argv[]) {
declare_array(numbers, 3, 10);
declare_array(larger_numbers, 6, 15);
print_array(larger_numbers);
// this works with blocks
if (numbers[0] < 5) {
print_array(numbers);
} else {
printf("Larger than 5\n");
}
// this does not work without blocks on if
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.