fields.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* fields.c -- define and use fields */
  2. #include <stdio.h>
  3. #include <stdbool.h> //C99, defines bool, true, false
  4. /* line styles */
  5. #define SOLID 0
  6. #define DOTTED 1
  7. #define DASHED 2
  8. /* primary colors */
  9. #define BLUE 4
  10. #define GREEN 2
  11. #define RED 1
  12. /* mixed colors */
  13. #define BLACK 0
  14. #define YELLOW (RED | GREEN)
  15. #define MAGENTA (RED | BLUE)
  16. #define CYAN (GREEN | BLUE)
  17. #define WHITE (RED | GREEN | BLUE)
  18. const char * colors[8] = {"black", "red", "green", "yellow",
  19. "blue", "magenta", "cyan", "white"};
  20. struct box_props {
  21. bool opaque : 1; // or unsigned int (pre C99)
  22. unsigned int fill_color : 3;
  23. unsigned int : 4;
  24. bool show_border : 1; // or unsigned int (pre C99)
  25. unsigned int border_color : 3;
  26. unsigned int border_style : 2;
  27. unsigned int : 2;
  28. };
  29. void show_settings(const struct box_props * pb);
  30. int main(void)
  31. {
  32. /* create and initialize box_props structure */
  33. struct box_props box = {true, YELLOW , true, GREEN, DASHED};
  34. printf("Original box settings:\n");
  35. show_settings(&box);
  36. box.opaque = false;
  37. box.fill_color = WHITE;
  38. box.border_color = MAGENTA;
  39. box.border_style = SOLID;
  40. printf("\nModified box settings:\n");
  41. show_settings(&box);
  42. return 0;
  43. }
  44. void show_settings(const struct box_props * pb)
  45. {
  46. printf("Box is %s.\n",
  47. pb->opaque == true ? "opaque": "transparent");
  48. printf("The fill color is %s.\n", colors[pb->fill_color]);
  49. printf("Border %s.\n",
  50. pb->show_border == true ? "shown" : "not shown");
  51. printf("The border color is %s.\n", colors[pb->border_color]);
  52. printf ("The border style is ");
  53. switch(pb->border_style)
  54. {
  55. case SOLID : printf("solid.\n"); break;
  56. case DOTTED : printf("dotted.\n"); break;
  57. case DASHED : printf("dashed.\n"); break;
  58. default : printf("unknown type.\n");
  59. }
  60. }