You can also use snprintf() to get the number of characters printed for the value this way:
char buff[100]; int width = snprintf (buff, sizeof(buff), "%d", number1);
But, however, you will get the desired width, you can simply use the format modifier * :
printf( "%*d %*d\n", width, number1, width, number2);
This modifier uses the following argument to specify a dynamic width, rather than one fixed in a format string.
source share