printf function returns the number of characters it printed on the console.
printf("\n%d", printf("Sololearn")) ;
see output that, this first prints Sololearn on console because of printf("Sololearn"), and returns 9 as it printed 9 characters so %d cause to print 9
Thats gonna return Hello5,
Hello for the print
5 for the number of characters it printed,
As for your question how it counts?
Let me demonstrate you by a pseudo code,
(Note:This my own understanding)
Int counter ->0;
for char x : string
Basically, it breaks the received String into char array, then prints one by one into the Standard Output stream, and every time it does that, it keeps track of number of time it printed through a counter variable