#include <stdio.h>
#include <string.h>
void formatNumberWithCommas(int n, char *output) {
char temp[64]; // Temporary buffer
sprintf(temp, "%ld", n); // Convert the number to a string
int length = strlen(temp);
int commas = (length - 1) / 3; // Calculate the number of commas
char *p = &temp[length]; // Pointer to the end of the number string
char *commasp = output + length + commas; // Pointer to the end of the output buffer
*commasp-- = *p--; // Null-terminate the output string
for (int i = 1; p >= temp; i++) {
*commasp-- = *p--; // Copy digits
if ((i % 3) == 0 && p >= temp) {
*commasp-- = ','; // Insert commas
}
}
}
int main(void) {
int n = 83907652;
char commas_number[64];
formatNumberWithCommas(n, commas_number);
puts(commas_number);
return 0;
}
/*
run:
83,907,652
*/