Format formatting specifier in printf

I have several lines of output, for example:

    printf("%-20s %-20s %-20s %-20s %-20s \n", "Identity", "Identity", "float", "double", "long double");
    printf("%-20s %-20s %-20s %-20s %-20s \n", "Number", "LHS", "error", "error", "error");

As you can see, if I wanted to change the interval between them, I would have to change the number 20 ten times. Is there a way to parameterize a format specifier? So that changing only once will change them all?

+4
source share
1 answer

Yes, you can make the field width an asterisk ( *) and specify the value as an argument int. Sort of

printf("%-*s \n", width, "Identity");

where widthhas a type intcontaining the value of the field width. You can change the value widthto change the width of the field.

C11 , Β§7.21.6.1, fprintf(),

. [...] * ( ) .

,

, , , , . int . [...]

+8

Source: https://habr.com/ru/post/1629217/


All Articles