fork(1) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. struct AnyType {
  6. int type;
  7. union {
  8. int i; // type 0
  9. double d; // type 1
  10. char c; // type 2
  11. } data;
  12. };
  13.  
  14. void printany(struct AnyType *p) {
  15. switch (p->type) {
  16. default: printf(" (--)"); break;
  17. case 0: printf(" %d", p->data.i); break;
  18. case 1: printf(" %f", p->data.d); break;
  19. case 2: printf(" %c", p->data.c); break;
  20. }
  21. }
  22.  
  23. int main(void) {
  24. struct AnyType array[5] = {0};
  25. array[0].type = 0; array[0].data.i = 3;
  26. array[1].type = 1; array[1].data.d = 2.5;
  27. array[2].type = 2; array[2].data.c = 'H';
  28. array[3].type = 0; array[3].data.i = 421;
  29. array[4].type = 2; array[4].data.c = 'a';
  30.  
  31. srand(time(0));
  32.  
  33. printf("random values:");
  34. for (int k = 0; k < 20; k++) {
  35. int index = rand() % 5; // ignore bias
  36. printany(&array[index]);
  37. }
  38. printf("\n");
  39. }
Success #stdin #stdout 0s 9424KB
stdin
Standard input is empty
stdout
random values: H 421 H 3 3 421 421 H 3 3 421 H H 421 H 2.500000 2.500000 2.500000 3 H