Guard a Macro Default
#define RACK_LIMIT 5 defines an object-like macro: later uses of the name are replaced with the token 5. The macro itself is not a typed variable and has no storage.
Suppose a rack holds five items by default, unless the build has already defined another limit. #ifndef checks for that definition before #define supplies the fallback; #endif closes the check. Unlike a macro, const int current is a typed object. Use const for an ordinary local value.
#include <stdio.h>
#ifndef RACK_LIMIT
#define RACK_LIMIT 5
#endif
int main(void) {
const int current = 3;
printf("Space: %d\n", RACK_LIMIT - current);
return 0;
}
With three occupied spots, Run prints Space: 2. If current were 4, the expression would give 1. #ifndef means “if not defined”; it checks whether a macro name exists, not whether its replacement value is zero.
The same pattern protects a local header from repeated inclusion. This is a header illustration, not another editor file:
#ifndef WORKSHOP_RECORDS_H
#define WORKSHOP_RECORDS_H
struct Record {
int count;
};
#endif
On the first inclusion, the macro is absent, so the declaration is included and the macro is defined. On later inclusions in the same compilation unit, the declaration is skipped. This header guard prevents repeated declarations that would otherwise conflict. Choose a distinct guard name for each header.