01.04.2018, 22:46
TIL you can specify a variable width or precision by using an asterisk.
This is useful in situations where you don't want to hardcode those values.
Код:
Width: printf("%5d", 10); //prints ' 10' printf("%*d", 5, 10); //prints ' 10' printf("%5f", 3.1415926535); //prints ' 3.141592' printf("%*f", 5, 3.1415926535); //prints ' 3.141592' Precision: printf("%.5f", 3.1415926535); //prints '3.14159' printf("%.*f", 5, 3.1415926535); //prints '3.14159' printf("%.5s", "abcdefghi"); //prints 'abcde' printf("%.*s", 5, "abcdefghi"); //prints 'abcde'