#include <stdio.h>

char *to_comma_str(int num, char *buf)
{
 if (num >= 1000) {
  char *next_pos = to_comma_str(num / 1000, buf);
  sprintf(next_pos, ",%03d", num % 1000);
  return next_pos + 4;
 } else {
  return buf + sprintf(buf, "%d", num);
 }
}