fork(1) download
  1. // Join items on output stream. (1.00)
  2.  
  3. #include <stdio.h>
  4.  
  5. void join(const void *base, size_t nitems, size_t itemsize,
  6. void (*putitem)(const void *item, FILE *stream),
  7. const char* separator, FILE *stream)
  8. {
  9. const char *first = base, *last = first + nitems*itemsize;
  10. if (first != last) {
  11. for (;;) {
  12. putitem(first, stream);
  13. first += itemsize;
  14. if (first == last) break;
  15. fputs(separator, stream);
  16. }
  17. }
  18. }
  19.  
  20. void put_int(const void *item, FILE *stream)
  21. {
  22. fprintf(stream, "%d", *(const int *) item);
  23. }
  24.  
  25. void print_ints(const int *a, size_t n, int end)
  26. {
  27. putchar('[');
  28. join(a, n, sizeof *a, put_int, ", ", stdout);
  29. putchar(']');
  30. putchar(end);
  31. }
  32.  
  33. int main(void)
  34. {
  35. int a[] = {1, 2, 3, 4};
  36. print_ints(a, sizeof a / sizeof *a, '\n');
  37. return 0;
  38. }
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
[1, 2, 3, 4]