Inserting spaces between digits in C

MusicianCPlus picture MusicianCPlus · Jan 18, 2010 · Viewed 25.4k times · Source

How would I go about taking a number like 123456 and having it print as 1 2 3 4 5 6?

Answer

John Carter picture John Carter · Jan 18, 2010

The simplest way of doing this (though not the fastest) would probably be to first sprintf the number to a string buffer, and then loop through the buffer printf-ing one character and one space at a time.

There's no built-in way of doing this within the standard printf formatting.