fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef int (*fx_p)(int, int);
  5.  
  6. int testadd(int a, int b) { return a+b; }
  7. int testsub(int a, int b) { return a-b; }
  8. int testmul(int a, int b) { return a*b; }
  9. int testdiv(int a, int b) { return a/b; }
  10.  
  11. fx_p getfx(int n) {
  12. switch (n) {
  13. default: return testadd;
  14. case 4: case 5: case 6: return testsub;
  15. case 7: case 8: return testmul;
  16. case 9: return testdiv;
  17. }
  18. }
  19.  
  20. int main(void) {
  21. // missing srand on purpose
  22. for (int k = 0; k < 20; k++) printf("%d\n", getfx(rand() % 10)(42, 10));
  23. return 0;
  24. }
  25.  
Success #stdin #stdout 0s 9424KB
stdin
Standard input is empty
stdout
52
32
420
32
52
32
32
52
4
52
52
420
52
4
52
32
52
32
52
32