Skip to content

Instantly share code, notes, and snippets.

@pstef
Created March 21, 2013 13:34
Show Gist options
  • Save pstef/5213034 to your computer and use it in GitHub Desktop.
Save pstef/5213034 to your computer and use it in GitHub Desktop.
Why I didn't like enum types in C until I learned about clang -Weverything -fsanitize=enum
#include <string.h>
#include <stdio.h>
enum car { honda };
enum number { ZERO, ONE };
enum number function_returning_number(char const *keyword) {
if (!strcmp(keyword, "zero"))
return ZERO;
if (!strcmp(keyword, "one"))
return ONE;
return -1; /* gcc and clang silent */
}
int main(void) {
enum number test = function_returning_number("one");
test = 1500; /* gcc and clang silent */
if (test == 1500) /* gcc silent, clang says this is always false */
puts("test == 1500");
test = honda; /* gcc silent, clang warns about implicit conversion */
if (test == honda) /* clang warns and gcc suddenly does too! */
puts("test == honda");
return test;
}
@pstef
Copy link
Author

pstef commented Mar 22, 2013

clang -Weverything -fsanitize=enum does the job!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment