Modular C
Module Config
A small configuration value can be centralized so functions agree on limits.
Module Config
module_config.c
#include <stdio.h>
enum {
MAX_ITEMS = 4
};
static int capped_count(int requested) {
if (requested > MAX_ITEMS) {
return MAX_ITEMS;
}
return requested;
}
int main(void) {
int requested = ;
int count = capped_count(requested);
printf("count=%d\n", count);
return 0;
}
#include <stdio.h>
enum {
MAX_ITEMS = 4
};
static int capped_count(int requested) {
if (requested > MAX_ITEMS) {
return MAX_ITEMS;
}
return requested;
}
int main(void) {
int requested = ;
int count = capped_count(requested);
printf("count=%d\n", count);
return 0;
}
#include <stdio.h>
enum {
MAX_ITEMS = 4
};
static int capped_count(int requested) {
if (requested > MAX_ITEMS) {
return MAX_ITEMS;
}
return requested;
}
int main(void) {
int requested = ;
int count = capped_count(requested);
printf("count=%d\n", count);
return 0;
}
shared limit
Centralizing a limit avoids repeating unexplained numbers in each function.
enum constant
An `enum` constant gives the compiler a named integer without preprocessor substitution.