Reputation: 91
i saw this code :
#include <stdio.h>
int main()
{
char string[] = " * * ** * * * *";
int line = 6, stop = 0, len = 8;
for(line = 6; line > 0;line--){
printf("%.*s\n", len, string + stop);
stop = stop + len;
--len;
}
return 0;
}
string is a character array, stop is an integer. how is %.*s selecting what to print? the out put is a tick mark made of starts:
*
*
*
* *
* *
*
Upvotes: 0
Views: 45
Reputation: 11648
Try this code and it will help explain what's going on...
include <stdio.h>
int main(void) {
char string[] = " * * ** * * * *";
int line = 6, stop = 0, len = 8;
for(line = 6; line > 0;line--){
printf("print %d chars starting at position %d\n", len, stop);
printf("%.*s\n", len, string + stop);
stop = stop + len;
--len;
}
return 0;
}
In the printf format ie %.*s
the .*
means that it is expecting an argument len
in your case to specify how many character to print. the string + stop
is specifying where to start printing from. I don't know who wrote this piece of code but it's a decent example of setting precision on strings.
Upvotes: 1