Reputation: 13
I want to put spaces in printf("%c",player.symbol);
spaces should be equal to the user input value in the following
printf("Next Move [1-%d]: ", game.pathlength);
scanf("%d", &player.location);
suppose player.symbol = V and user input Next Move: 8 i.e player.location =8 then the output should be like this: (8 spaces)V i.e, there must be 8 spaces before V.
How can I do that?
Upvotes: 1
Views: 962
Reputation: 12698
You can printf()
an arbitrary number of spaces by controlling the variable width field character *
in the following way:
void print_spaces(int n)
{
printf("%*s", n, "");
}
So you print an empty string in a field of size the number of spaces you request it to have.
Upvotes: 0
Reputation: 14046
First, things to know from the printf manual:
The field width
An optional decimal digit string (with nonzero first digit) specifying a minimum field width. If the converted value has fewer characters than the field width, it will be padded with spaces on the left ... Instead of a decimal digit string one may write "*" ... to specify that the field width is given in the next argument
With that in mind to achieve the left space padding you require the code would be:
printf("%*c", player.location+1, player.symbol);
This says that the minimum width of the print is player.location+1
. Since the player.symbol
is a single character that means there will be player.location
of space padding.
Upvotes: 1