Yes it is possible:
printf("\n| %.*s | %8s | %8s |", MAX, "Nome", "Salario", "Idade");
The secret is .*
. It is a parameter to the string formatting. Note that since it is the first parameter the MAX
must be the first one in the list, that is, it follows the same logic as the other normal parameters of printf
. With .*
it is a limiter of the maximum characters that will be displayed which is what I understood you want. I have still put options to use the parameter as the minimum of characters that should be displayed using only *
.
Read the documentation for more details. Especially on "The field width". And a summary about existing formatting parameters.
#include <stdio.h>
int main(void) {
int max = 30;
printf("\n| %*s | %8s | %8s |", max, "Nome", "Salario", "Idade");
printf("\n| %.*s | %8s | %8s |", max, "Nome", "Salario", "Idade");
max = 3;
printf("\n| %*s | %8s | %8s |", max, "Nome", "Salario", "Idade");
printf("\n| %.*s | %8s | %8s |", max, "Nome", "Salario", "Idade");
}
See running on ideone . And in Coding Ground . Also I placed GitHub for future reference .