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 | #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);
// BUG: the first line of the macro is the only
// one in a single line if statment
if (numbers[0] < 5)
print_array(numbers);
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.