17_enum.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include <stdio.h>
  2. enum fred
  3. {
  4. a,
  5. b,
  6. c,
  7. d,
  8. e = 54,
  9. f = 73,
  10. g,
  11. h
  12. };
  13. /* All following uses of enum efoo should compile
  14. without warning. While forward enums aren't ISO C,
  15. it's accepted by GCC also in strict mode, and only warned
  16. about with -pedantic. This happens in the real world. */
  17. /* Strict ISO C doesn't allow this kind of forward declaration of
  18. enums, but GCC accepts it (and gives only pedantic warning), and
  19. it occurs in the wild. */
  20. enum efoo;
  21. struct Sforward_use {
  22. int (*fmember) (enum efoo x);
  23. };
  24. extern enum efoo it_real_fn(void);
  25. enum efoo {
  26. ONE,
  27. TWO,
  28. };
  29. struct S2 {
  30. enum efoo (*f2) (void);
  31. };
  32. void should_compile(struct S2 *s)
  33. {
  34. s->f2 = it_real_fn;
  35. }
  36. enum efoo it_real_fn(void)
  37. {
  38. return TWO;
  39. }
  40. static unsigned int deref_uintptr(unsigned int *p)
  41. {
  42. return *p;
  43. }
  44. enum Epositive {
  45. epos_one, epos_two
  46. };
  47. int main()
  48. {
  49. enum fred frod;
  50. enum Epositive epos = epos_two;
  51. printf("%d %d %d %d %d %d %d %d\n", a, b, c, d, e, f, g, h);
  52. /* printf("%d\n", frod); */
  53. frod = 12;
  54. printf("%d\n", frod);
  55. frod = e;
  56. printf("%d\n", frod);
  57. /* Following should compile without warning. */
  58. printf ("enum to int: %u\n", deref_uintptr(&epos));
  59. return 0;
  60. }
  61. /* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/