Use the non-standard apostrophe
flag in the printf format string, if you have that option available and don't mind losing a little bit of portability.
According to my documentation, the '
flag is available for POSIX systems since 1997.
If you are on Unix, Linux, Mac, ... you should have no problem
If you are on Windows, DOS, iSeries, Android, ... all bets are off (but maybe you can install a POSIX layer to your system).
#include <locale.h>
#include <stdio.h>
int main(void) {
long int x = 130006714000000;
setlocale(LC_NUMERIC, "en_US.utf-8"); /* important */
while (x > 0) {
printf("# %%'22ld: %'22ld\n", x); /* apostrophe flag */
x *= 2; /* on my machine, the Undefined Behaviour for overflow
// makes the number become negative with no ill effects */
}
return 0;
}
On my system this program produces:
# %'22ld: 130,006,714,000,000
# %'22ld: 260,013,428,000,000
# %'22ld: 520,026,856,000,000
# %'22ld: 1,040,053,712,000,000
# %'22ld: 2,080,107,424,000,000
# %'22ld: 4,160,214,848,000,000
# %'22ld: 8,320,429,696,000,000
# %'22ld: 16,640,859,392,000,000
# %'22ld: 33,281,718,784,000,000
# %'22ld: 66,563,437,568,000,000
# %'22ld: 133,126,875,136,000,000
# %'22ld: 266,253,750,272,000,000
# %'22ld: 532,507,500,544,000,000
# %'22ld: 1,065,015,001,088,000,000
# %'22ld: 2,130,030,002,176,000,000
# %'22ld: 4,260,060,004,352,000,000
# %'22ld: 8,520,120,008,704,000,000