Name a Status with enum
The union section used an enum to label the stored alternative. The same idea works for an ordinary status: a loan record can be stored or borrowed. Naming those states with enum State makes state == BORROWED clearer than testing an unexplained number.
The names inside the braces are enumerators, integer constants. With no explicit values, STORED is 0 and BORROWED is 1. You can choose a starting value, as in enum Level { LOW = 3, HIGH };, where HIGH becomes 4. An enum variable can still hold other integer values in C, so check values that come from outside the program.
#include <stdio.h>
enum State {
STORED,
BORROWED
};
int main(void) {
enum State state = BORROWED;
printf("Borrowed: %d\n", state == BORROWED);
return 0;
}
The comparison is true, so Run prints Borrowed: 1. If the initializer used STORED, the comparison would print 0. The enum name is not a string: C does not automatically print the word BORROWED for you.