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 43 44 45 46 | #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#define log_int(n) printf("%s == %d\n", #n, n)
enum ClassStatus {
FRESHMAN, SOPHOMORE, JUNIOR, SENIOR, SUPER_SENIOR
};
const char* get_status_string(enum ClassStatus status) {
switch(status) {
case FRESHMAN: return "freshman";
case SOPHOMORE: return "sophomore";
case JUNIOR: return "junior";
case SENIOR: return "senior";
case SUPER_SENIOR: return "super senior";
default: return "unknown class status";
}
}
int main(int argc, char* argv[]) {
enum ClassStatus status = JUNIOR;
log_int(status);
printf("Status is %s\n", get_status_string(status));
printf("Size of status is %ld\n", sizeof(status));
// nothing prevents me from using an "invalid" status
// but in most cases this a bad idea
enum ClassStatus status2 = 7;
log_int(status2);
printf("Status is %s\n", get_status_string(status2));
// BAD code quality: should use the constant instead of the number
// similar to using a character ascii value instead of a character constant
enum ClassStatus status3 = 2;
log_int(status3);
printf("Status is %s\n", get_status_string(status3));
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.