Skip to content

Instantly share code, notes, and snippets.

@elieux
Created October 7, 2018 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elieux/3ee7a0073ce166aa4da9342e6840a93d to your computer and use it in GitHub Desktop.
Save elieux/3ee7a0073ce166aa4da9342e6840a93d to your computer and use it in GitHub Desktop.
libweek - something like an enum, but with values not known in the source code
#include <stdio.h>
#include <libweek.h>
static char* boolstr(bool b) {
return b ? "weekend, yay" : "a weekday";
}
int main() {
printf("Monday (%d) is %s.\n", MONDAY, boolstr(is_weekend(MONDAY)));
printf("Tuesday (%d) is %s.\n", TUESDAY, boolstr(is_weekend(TUESDAY)));
printf("Wednesday (%d) is %s.\n", WEDNESDAY, boolstr(is_weekend(WEDNESDAY)));
printf("Thursday (%d) is %s.\n", THURSDAY, boolstr(is_weekend(THURSDAY)));
printf("Friday (%d) is %s.\n", FRIDAY, boolstr(is_weekend(FRIDAY)));
printf("Saturday (%d) is %s.\n", SATURDAY, boolstr(is_weekend(SATURDAY)));
printf("Sunday (%d) is %s.\n", SUNDAY, boolstr(is_weekend(SUNDAY)));
return 0;
}
#include "libweek.h"
const day_t MONDAY = 45;
const day_t TUESDAY = 84;
const day_t WEDNESDAY = 63;
const day_t THURSDAY = 37;
const day_t FRIDAY = 99;
const day_t SATURDAY = 102;
const day_t SUNDAY = 111;
bool is_weekend(day_t day) {
return day == SATURDAY || day == SUNDAY;
}
#include <stdbool.h>
typedef int day_t;
extern const day_t MONDAY;
extern const day_t TUESDAY;
extern const day_t WEDNESDAY;
extern const day_t THURSDAY;
extern const day_t FRIDAY;
extern const day_t SATURDAY;
extern const day_t SUNDAY;
bool is_weekend(day_t day);
all:
gcc -Wall -Wextra -Werror -std=c99 -shared libweek.c -o libweek.dll
gcc -Wall -Wextra -Werror -std=c99 -L. -I. app.c -o app.exe -lweek
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment